paint-brush
"Specified key was too long; max key length is 767 bytes" Error in Laravelby@spekulatius
567 reads
567 reads

"Specified key was too long; max key length is 767 bytes" Error in Laravel

by PeterAugust 17th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

An issue is coming up when using older versions of MySQL (prior to 5.7.8) or MariaDB. This can be resolved in Laravel simply by changing your app service provider configuration. After this you might need to rebuild your database using. This will completely reset your database and start off fresh. Alternatively, you can also upgrade your database server. This hasn't been discussed here as it is very system specific. This isn't discussed here but it's very system-specific.
featured image - "Specified key was too long; max key length is 767 bytes" Error in Laravel
Peter HackerNoon profile picture

One of these issues you might stumble across using the PHP framework Laravel is this one:

Specified key was too long; max key length is 767 bytes

After some research, I've found out it's coming up when
using older versions of MySQL (prior to 5.7.8) or MariaDB (prior to
10.2.2). It was MariaDB in my case.

Solution #1:

In Laravel this can be resolved simply by changing your app service provider configuration (in app/Providers/AppServiceProvider.php) with the following:


use Illuminate\Support\Facades\Schema;

public function boot()
{
    /**
     * Ensure we aren't running into `Specified key was too long; max key length is 767 bytes`
     *
     * @see https://peterthaleikis.com/posts/laravel:-specified-key-was-too-long-max-key-length-is-767-bytes/
     **/
    Schema::defaultStringLength(191);
}

After this you might need to rebuild your database using

php artisan migrate:fresh --seed 

- this will completely reset your database and start off fresh.

Solution #2:

You can also upgrade your database server. This hasn't been discussed here as it is very system specific.

DuckDuckGo is your friend 💪️

Previously published on https://peterthaleikis.com/posts/laravel:-specified-key-was-too-long-max-key-length-is-767-bytes/.