Migration error on Laravel 5.4 with php artisan make:auth
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter tabl e
users
add uniqueusers_email_unique
([PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
Add the below code in
app/Providers/AppServiceProvider.php
method: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 and then after deleting old tables run
php artisan migrate
commandIn order to avoid changing anything in your code, simply update your MySQL server to at least 5.7.7
Reference this for more info : https://laravel-news.com/laravel-5-4-key-too-long-error
I don't know why the above solution and the official solution which is adding
in
AppServiceProvider
didn't work for me. What worked for was editing thedatabase.php
file inconfig
folder. Just editto
and it should work. Hope it helps.
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:Note: 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:After that everything should work as normal.
I think to force StringLenght to 191 is a really bad idea. So I investigate to understand what is going on.
I noticed that this message error :
Started to show up after I updated my MySQL version. So I've checked the tables with PHPMyAdmin and I've noticed that all the new tables created were with the collation utf8mb4_unicode_ci instead of utf8_unicode_ci for the old ones.
In my doctrine config file, I noticed that charset was set to utf8mb4, but all my previous tables were created in utf8, so I guess this is some update magic that it start to work on utf8mb4.
Now the easy fix is to change the line charset in your ORM config file. Then to drop the tables using utf8mb4_unicode_ci if you are in dev mode or fixe the charset if you can't drop them.
For Symfony 4
Now my doctrine migrations are working again just fine.