Specified key was too long max key length is 767 bytes

When you run the migrations for the first time in Laravel, you may have experience this little tiny error saying something like Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

[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))

This Specified key was too long max key length is 767 bytes error happens because Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4.

According to the Laravel official docs what you have to do to get rid of this is just to edit the AppServiceProvider.php file in your Laravel installation.

This file can be found at

app\Providers\AppServiceProvider.php

Find the  boot() method and add the following line of code.

Schema::defaultStringLength(191);

That’s it. And make sure you import Schema Facade So, the full code will be like this.

use Illuminate\Support\Facades\Schema;

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

That’s it, and enjoy Laravel!

Video Tutorial for this


Food for thought: Setting up Cron Jobs in Laravel

Leave a Reply

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