Laravel Migration Error: Specified Key Was Too Long; Max Key Length is 767 Bytes

by Adam Patel

So you’ve got your database connected up to your app and now you try to run your initial database migration – but it doesn’t work. You get an error:

Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Laravel is starting to seem like hard work now, right? For every step, there’s another headache.

The solution is to add two lines of code to the public boot function, located in AppServiceProvider.php (App/Providers/AppServiceProvider.php)

At the top of the file, under namespace, add

use Illuminate\Support\Facades\Schema;

Without this line, the second line will not work and it will produce an error when you try to run the migration. The second line is the line starting ‘Schema’, which should be added to the boot function. I’ve printed the whole boot function below for context (because there’s no point knowing what code to add if you don’t know where to add it).

public function boot()

The migration should now run properly. And thankfully, laravel starts to behave a bit more smoothly from this point onwards.

Copyright 2021. All Rights Reserved. Adam Patel