Repository navigation
[5.4] SQL error when migrating tables #17508
Description
Activity
In your appserviceprovider boot method, try adding
Schema::defaultStringLength(191);Best sure to import Illuminate\Support\Facades\Schema at the top of the service provider.
There's a note about this in the 5.4 documentation. It's possible this is what is causing your problems.Reacted by Thilina Ashen Gamage, Surya Oktafendri, Moamen Basel, Christopher Dosin, Khoa, Akshay Jumbade, philliperosario, Jonas H., César Augusto, Pasindu Dilshan and 196 moreReacted by Oliver Cooper, Prakhar Agarwal, Jake Lacey, robert van bekkum, Ped, Louis Auneau, Julien GARDET, EzequielValencia and ibrahim4145Reacted by Christopher, Ewomazino Ukah, Armando, Aisha Badar, Jaimin Patel, Phillip Roth, Krunal, Eduardo Alvarado D, Keyur Kansara, Jaffer Wilson and 6 moreReacted by Aisha Badar, bbonlinesolutions, Phillip Roth, Inorinne, Jim Paulo Ovejera, RACHELRIGGS, Krunal, Eduardo Alvarado D, Jaffer Wilson, Moh'd Alkhunaizi and 10 moreReacted by ZeTryall, David, Abel Lamesgen, Julien GARDET, Hassan Salem and Abhishek SharmaReacted by Markus Rafferty, Idris Fab, amin, Behnam, Ma. Angelica B. Concepcion, mewcrow, Brian Matovu, Ashar Khan, dalink, RC and 46 moreThank 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
boot()method) despite running MySQL 5.7.14 on that computer. The documentation says that there could be an issue with versions before 5.7.7, but I'm having a problem with a newer version as well.Reacted by Fábio Duque Silva, José Cage , Emre Berber, vidann1 and jcWhy 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
innodb_large_prefixthis will let you have 255 fields on your database you won't need to touch any code.On these other versions you don't need, it works out of the box.
Reacted by josephyanni, Thilina Ashen Gamage, Mircea Nicolae, Daravattanak Ros, Akeem Amusat, dlopzz, cmykm, José Cage , Albi Dudija and Mohamed CHIBANIReacted by Ant Na and Henri DevigneReacted by Sachin and Henri DevigneI got this error too with MySQL 5.7.11.
As @devcircus said, the workaround on https://laravel.com/docs/5.4/migrations#indexes works.
UPDATE
According to @rbkkm, to fix this issue, you can decrease the index rather than the field size:
This lets you use the full 255 characters at the "expense" of index length, rather than getting the full index length at the expense of the varchar field length.
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
password_resets'sup()method would look like this: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
users: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)')]); });
Reacted by EZaca, Aleksander Kochetkov, Ped, Ersin Yılmaz, Jayce Guo, Jonatas Rodrigues, Piyumika Sewandi, Alfonso Delgado and Carlos RodriguesReacted by Ped, Leoon, Jayce Guo and Carlos RodriguesReacted by Ped, Jayce Guo, nexus4best and Carlos RodriguesReacted by Ped, Ali Bakr, Jayce Guo, ajiKusuma and Jonatas Rodrigues@devcircus : why using 191 ?
Reacted by Ped, Josh Strange, Nehal, David Craig, Mohamed Elwan, Andrés David Montoya Aguirre, Chris De David, xjpeng, Francesco Mussi, James Wiley and 14 moreGot this Error too with Laravel 5.4.9, Spark 4.0, Valet v2.0.3 & 10.1.16-MariaDB Homebrew.
AddingSchema::defaultStringLength(191);in the AppServiceprovider boot Method worked for me in this Case.Reacted by ngooide, Eric Rosenberg, Bijaya Prasad Kuikel, Mohammad Abu Sufian, Caio Vinicius, Francesco Mussi, WTFranklin and Simon Muraya@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.
Reacted by Ped, Brandon Simpson, Khoa, Joe Dawson, Serhii Matrunchyk, Cameron Macfarlane, Clément Rigo, Vince Oveson, Matthieu Napoli, Adrian Rončević and 3 moreJust had a fresh install, still getting this problem. Adding the
Schema::defaultStringLength(191);that @devcircus was the trick.Reacted by Clayton Stone and Simon MurayaThanks @devcircus your solution solved my problem.
Reacted by Clayton Stone and Simon Muraya@devcircus Thank you!!!
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.
Reacted by Khoa, Joe Dawson, Martijn, Gerry Palad, Yassine Sedrani and PedI added to the migration itself
Schema::defaultStringLength(191); Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });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
Reacted by Nermin B., Janne Kurkinen, Cas Bloem, mario, Manoj Thapliyal, Chris De David, Phillip Roth, Ricardo Caro, Kun, Al-Amin Sarker and 24 moreReacted by jonathan tavio and Ahmed KhalidReacted by David Craig, Antoine B., Benyamin Bolhassani and PedReacted by Kun, fcovaldez, Clasence Neba Shu, JOSE CRUZ CHAVEZ, Jakhongir91, Mairo Rodrigues and Ahmed Khalid66 remaining items
Please add
use Illuminate\Support\Facades\Schema;
boot(){
Schema::defaultStringLength(191);
}
to your app/provider/AppServiceProvider.php- 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.
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter tableltm_translationsadd uniqueixk_ltm_translations_locale_group_key(locale,group,key))mysql is 5.7.21, Laravel Framework 5.5.35
Content of AppServiceProvider.php is:
`namespace App\Providers;use Auth;
use Carbon\Carbon;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
// set correct locale for Carbon
Carbon::setLocale(config('app.locale'));
}/** * Register any application services. * * @return void */ public function register() { // }}
`File: config/database.php
change the following
FROM ->
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',TO ->
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',Reacted by Moisés WendtReacted by André SilvaHi there. I hope can understandme.
I solved to follow this steps. in laravel-project/databases/migrations
edit both files (create_users_table, create_password_reset_table) in function up().
like this:public function up() { 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)')]); }); }Tested on:
Ubuntu 16.04
10.1.13-MariaDB
PHP 7.2.7-1In 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:
This lets you use the full 255 characters at the "expense" of index length, rather than getting the full index length at the expense of the varchar field length.
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
password_resets'sup()method would look like this: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
users: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 .
Reacted by PedAs 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:
use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }first you have to delete (if you have) users table, password_resets table from the database and delete users and password_resets entries from migrations table.
To run all of your outstanding migrations, execute the migrate Artisan command:
php artisan migrateReacted by Ped and Mark SnapeIlluminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table
usersadd uniqueusers_email_unique(email))Answer:
Dir:
App/Providers/AppServiceProvider.phpAdd line in Top:
use Illuminate\Support\Facades\Schema;Then:
public function boot()
{
//
Schema::defaultStringLength(191);
}Reacted by Ped- Laravel Version: 5.4.0
- PHP Version: 7.0.12
- Database Driver & Version: MySQL 5.6.33
Description:
When I create a new 5.4 project and try to migrate the database tables, I get this SQL error :
[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
usersadd uniqueusers_email_unique(email))I get this error when running
php artisan migrateon 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:
- Create a new project (laravel new project, with installer 1.3.3)
cdinto the project- Fill in the database information in the
.envfile - Run
php artisan migrate
This issue is still there with Laravel 5.7.17.
Why this issue is not solved with an update.Search the issues. This has been discussed many times.
Reacted by PedLaravel 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.
- locked and limited conversation to collaborators
on Mar 4, 2019
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 migrateon 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:
cdinto the project.envfilephp artisan migrate