Laravel 5.4 migrations & max key length

I’ve recently switched to a Laravel 5.4 project. After trying to run migrations, I kept getting this darn error: 

[PDOException]
 SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

It made no sense. I have run this migration on a 5.2 Laravel project and was fine. After googling, I found out the reason why wasn’t because of the migration, but the database collation. On the previous project, I had utf8_unicode_ci and this one is using utf8mb4_unicode_ci which was better when trying to deal with multiple languages. Well fiddle sticks. What to do? I found the answer. 

Add this line:

Schema::defaultStringLength(191);

…to the AppServiceProvider.php. Ran the migration again and worked like a charm!

Leave a Reply

Your email address will not be published. Required fields are marked *