EN ES
Home > Web development > Laravel Tutorials > Laravel 5.4 Specified Key Was Too Long Max Key Length Is 767 Bytes Error

Laravel 5.4 Specified Key Was Too Long Max Key Length Is 767 Bytes Error

Diego Cortés
Diego Cortés
May 17, 2017
Laravel 5.4 Specified Key Was Too Long Max Key Length Is 767 Bytes Error

Hello everyone!

Laravel 5.4 itself brings many improvements and beneficial changes for all of us who use it, but in my case this error has happened to me on more than one occasion when trying to execute any of these two commands:

php artisan migrate

php artisan migrate --seed

Error showing:

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

There are 2 solutions, the first time option 1 worked for me, but in another project it simply didn't help me at all so option 2 came to save me 😀

Option 1:

This first option is given directly in the Laravel documentation

Go to the AppServiceProvider.php file, located at:

app/Providers/AppServiceProvider.php

and in the boot function we must add the following line

Schema::defaultStringLength(191);

finally remaining like this:

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
    Schema::defaultStringLength(191);
}

Option 2:

For this option we must edit the Laravel configuration for MYSQL in the database.php file located at:

config/database.php

once there we will look for the mysql option and we must change the charset and collation values

'mysql' => [
    'charset' => 'utf8mb4',
    'collation'=>'utf8mb4_unicode_ci',
]

by the following:

'mysql' => [
    'charset' => 'utf8',
    'collation'=>'utf8_unicode_ci',
]

At least they worked perfectly for me, try the first one, if it doesn't work for you try the second one and problem solved! 😀

More information about the problem:

The problem is because the character set was changed to utf8mb4 which allows storing emojis, which is only supported from MYSQL in its version 5.7.7 so if your version is lower you will get the problem so you know!

I hope it helps more than one person 😀

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Categories

Page loaded in 25.86 ms