Laravel 5.6 : Specified key was too long error

Laravel 5.6 : Specified key was too long error

“Laravel 5.6 : Specified key was too long error”

is our main topic for this article.

As you know laravel is one of the best and favourite framework we have but Laravel 5.6 made a change to their default database character set, and it’s now utf8mb4 which support for storing emojis.

If you are running latest version of laravel and you are running MySQL v5.7.* and higher you do not need to do anything.

But if you are running MariaDB or older version of MySQL you may face this error when you trying to run php artisan migrate :

Laravel 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 users add unique users_email_unique(email))

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

Solution :

As mentioned in official documentation, to fix this all you need to do is edit your AppServiceProvider.php file and inside the boot method set a default string lenght :

AppServiceProvider.php

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

That’s it. after this everything works normally and you can build awesome things 🙂 .

If you wat to install laravel through composer then please refer this link or if you want to build to-do application using laravel hen please refer this link.

If you like this post please share and also join our news letter. Thanks.

Happy Codding! Cheers!

Adam Brown
Please follow and like us:

Leave a Comment