-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.4] SQL error when migrating tables #17508
Comments
In your appserviceprovider boot method, try adding
Best sure to import Illuminate\Support\Facades\Schema at the top of the service provider. |
Thank you @devcircus , adding this line fixed my issue. It should be noted however that the problem occurs on my Windows computer (if I don't add the extra line of code in the |
Why is this closed? The issue is still there? Using homestead I still get this error. |
Check the 5.4 docs in the database / migration section. It's been covered several times in the last few days here, on slack and on the laracasts forum as well. |
If you are on Mysql < 5.7 or MariaDB < 10.2 then enable On these other versions you don't need, it works out of the box. |
I got this error too with MySQL 5.7.11. As @devcircus said, the workaround on https://laravel.com/docs/5.4/migrations#indexes works. UPDATEAccording to @rbkkm, to fix this issue, you can decrease the index rather than the field size:
What @rbkkm seems to be suggesting is this: $table->index([DB::raw('email(191)')]);
$table->unique([DB::raw('email(191)')]); So for example, the migration of Schema::create('password_resets', function (Blueprint $table) {
$table->string('email');
$table->string('token');
$table->timestamp('created_at')->nullable();
$table->unique([DB::raw('email(191)')]);
}); And for the migration of Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->index([DB::raw('email(191)')]);
}); |
@devcircus : why using 191 ? |
Got this Error too with Laravel 5.4.9, Spark 4.0, Valet v2.0.3 & 10.1.16-MariaDB Homebrew. |
@devcircus Thank you!!! |
Should this not be considered an open problem? I understand that it's a documented problem, but shouldn't we expect some level of backwards compatibility with homestead? My homestead version is almost a year old and a new deploy caused this problem for me. The above solution solved it for me. |
Just had a fresh install, still getting this problem. Adding the |
Thanks @devcircus your solution solved my problem. |
Please, guys. Do not post comments if it does not add anything of value to the issue thread. Every subscriber to the issue gets a useless email. Show your thanks by using the 👍 emoji like the others did. |
I added to the migration itself
yes, I know I need to consider it on every migration but I would rather that than have it tucked away in some completely unrelated service provider |
Then make it related? Service providers are great for that sort of thing. If you still don't want to, you could also make a different parent class, although I think that that would need a manual change each time you run |
@snapey I like Snapey's solution |
This is an issue on fresh installs, Laravel Framework 5.4.15, MacBook Pro, Server version: 5.6.23 MySQL Community Server |
AppServiceProvider.php It has been modified
But still is error!
|
In case someone else ends up here first and still banging his head, the whole solution for Maria/MySQL below 10.2/5.7 is explained here: For me, the one part that did the trick after correctly setting everything else and which I was still missing until I found that post, is:
|
I've also experience this today, given that I already included @zanjs have you fixed this?! We have the same issue. |
Check that your AppServiceProvider.php It has been modified to include what is in bold: namespace App\Providers; use Illuminate\Support\ServiceProvider; |
@sicaps thanks for your reply. I also have that in my AppServiceProvider.php. Anyway, I manage to fix it by removing the length of the string, before one of my index is like this |
solved mine by changing the collation in the database.php file located in the config folder of my project to 'charset' => 'utf8', |
@fernandobandeira Here you go: laravel/docs#4075. |
Fixes: #1927. Related: laravel/framework#17508. Issue occurs when database configuration related to full support for the utf8mb4 charset is incorrect; MySQL > 5.7 & MariaDB > 10.2 doesn't have this issue because they default to the correct configuration values; this fix solves the issue for older versions of MySQL and MariaDB without requiring database server configuration changes. The root cause of the issue with the utf8mb4 encoding is that both InnoDB and MyISAM have too low of an index key prefix limit (767 bytes and 1000 bytes respectively) to properly store 255 4-byte characters; which would take 1024 bytes. See the docs on InnoDB limitations: https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html In MySQL >= 5.7 & MariaDB >= 10.2 this limit has been bumped to 3076 bytes by the changing of the default value of the `innodb_large_prefix` configuration property (introduced in MySQL 5.5) to true; which is what bumps up the limit. In order to manually set that property to true on earlier versions, `innodb_file_format` must be set to `BARRACUDA` and `row_format` must be `DYNAMIC` or `COMPRESSED`. See http://mechanics.flite.com/blog/2014/07/29/using-innodb-large-prefix-to-avoid-error-1071/ for more information. This change fixes the issue by changing the default string length to 191 (total of 764 bytes, within the older size limit) when the MySQL database config is detected to be using the utf8mb4 charset.
Hi everyone, in the version of Laravel 5.6 I've fix the issue changing the .env file in the line DB_HOST=127.0.0.1 |
@jamel2020 Just use @rbkkm's solution, as I described here: #17508 (comment). |
wer i creat it !!! im not good in laravel |
exemple plzzz |
thnx its good i find |
thanks to all specially zanjs due to it code help me |
I made a video to help beginner to fix the problem: https://youtu.be/LzD3Cda7GuY |
Fixes: octobercms/october#1927. Related: laravel/framework#17508. Issue occurs when database configuration related to full support for the utf8mb4 charset is incorrect; MySQL > 5.7 & MariaDB > 10.2 doesn't have this issue because they default to the correct configuration values; this fix solves the issue for older versions of MySQL and MariaDB without requiring database server configuration changes. The root cause of the issue with the utf8mb4 encoding is that both InnoDB and MyISAM have too low of an index key prefix limit (767 bytes and 1000 bytes respectively) to properly store 255 4-byte characters; which would take 1024 bytes. See the docs on InnoDB limitations: https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html In MySQL >= 5.7 & MariaDB >= 10.2 this limit has been bumped to 3076 bytes by the changing of the default value of the `innodb_large_prefix` configuration property (introduced in MySQL 5.5) to true; which is what bumps up the limit. In order to manually set that property to true on earlier versions, `innodb_file_format` must be set to `BARRACUDA` and `row_format` must be `DYNAMIC` or `COMPRESSED`. See http://mechanics.flite.com/blog/2014/07/29/using-innodb-large-prefix-to-avoid-error-1071/ for more information. This change fixes the issue by changing the default string length to 191 (total of 764 bytes, within the older size limit) when the MySQL database config is detected to be using the utf8mb4 charset.
Fixes: octobercms/october#1927. Related: laravel/framework#17508. Issue occurs when database configuration related to full support for the utf8mb4 charset is incorrect; MySQL > 5.7 & MariaDB > 10.2 doesn't have this issue because they default to the correct configuration values; this fix solves the issue for older versions of MySQL and MariaDB without requiring database server configuration changes. The root cause of the issue with the utf8mb4 encoding is that both InnoDB and MyISAM have too low of an index key prefix limit (767 bytes and 1000 bytes respectively) to properly store 255 4-byte characters; which would take 1024 bytes. See the docs on InnoDB limitations: https://dev.mysql.com/doc/refman/5.7/en/innodb-restrictions.html In MySQL >= 5.7 & MariaDB >= 10.2 this limit has been bumped to 3076 bytes by the changing of the default value of the `innodb_large_prefix` configuration property (introduced in MySQL 5.5) to true; which is what bumps up the limit. In order to manually set that property to true on earlier versions, `innodb_file_format` must be set to `BARRACUDA` and `row_format` must be `DYNAMIC` or `COMPRESSED`. See http://mechanics.flite.com/blog/2014/07/29/using-innodb-large-prefix-to-avoid-error-1071/ for more information. This change fixes the issue by changing the default string length to 191 (total of 764 bytes, within the older size limit) when the MySQL database config is detected to be using the utf8mb4 charset.
Please add |
Dear YayaDy,
Thank you for your replying, I did it, it works perfect!
Good day,
… On May 25, 2018, at 4:44 PM, YayaDy ***@***.***> wrote:
Please add
use Illuminate\Support\Facades\Schema;
boot(){
Schema::defaultStringLength(191);
}
to your app/provider/AppServiceProvider.php
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#17508 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AHxWoNFf9mhQFcd4Zj58ECJ3jpvEvZwMks5t19JjgaJpZM4LsiKQ>.
|
Still have the same problem with a fresh installation. Diffrence is, max key length is 1000 bytes instead of 767 bytes. mysql is 5.7.21, Laravel Framework 5.5.35 use Auth; class AppServiceProvider extends ServiceProvider
} |
File: config/database.php TO -> |
Hi there. I hope can understandme.
Tested on: |
In your appserviceprovider boot method, try adding Schema::defaultStringLength(191); Dont Forget to copy paste " Illuminate\Support\Facades\Schema; " at the top of the service provider. |
@themsaid This issue thread does not get any more useful comments. People are just repeating things. I suggest closing and locking it. P.S. I still find it unreasonable not to mention this more thoroughly in the documentation as I proposed here: laravel/docs#4075. To summarize, I think the best solution is provided by @rbkkm, which I explained way up (in this thread): #17508 (comment). According to @rbkkm, to fix this issue you can decrease the index rather than the field size:
What @rbkkm seems to be suggesting is this: $table->index([DB::raw('email(191)')]);
$table->unique([DB::raw('email(191)')]); So for example, the migration of Schema::create('password_resets', function (Blueprint $table) {
$table->string('email');
$table->string('token');
$table->timestamp('created_at')->nullable();
$table->unique([DB::raw('email(191)')]);
}); And for the migration of Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->index([DB::raw('email(191)')]);
}); |
I think this is the answer . |
As outlined in the Migrations guide to fix this all you have to do is edit your app/Providers/AppServiceProvider.php file and inside the boot method set a default string length:
To run all of your outstanding migrations, execute the migrate Artisan command:
|
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table Answer: Add line in Top: Then: |
This issue is still there with Laravel 5.7.17. |
Search the issues. This has been discussed many times. |
Laravel 5.7 - define length in email public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email', 191)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
} |
I'm locking this issue because it either has gone off-topic, become a dumping ground for things which shouldn't be in an issue tracker or is just too old. |
Description:
When I create a new 5.4 project and try to migrate the database tables, I get this SQL error :
I get this error when running
php artisan migrate
on a fresh install on a Macbook, on my Windows computer I get the same error except it says that the max length is 1000 bytes instead of 767 bytes.Steps To Reproduce:
cd
into the project.env
filephp artisan migrate
The text was updated successfully, but these errors were encountered: