After Laravel made changes to its default database character set, the specified key was too long issue was supposed to have been taken care of. However, today as I was creating a new Laravel application, to my surprise, I again came across this error. I ran across this error when I was trying to run migration for the first time after generating the auth scaffolding.
This was a surprise because this was supposed to have been fixed long back. And after doing some reading around, I found that it indeed has been fixed. At least that is what is being said. But I somehow came across it even in Laravel 7.x!
I am not sure if it has got something to do with the stack that I am using (I don’t think so) as I am using a simple PHP-MySQL combo. Nothing fancy.
How to fix Laravel Specified key was too long error?
The trick to fix this error is still the same, just the way it was in the previous versions of Laravel. This is how to do it:
- Navigate to the AppServiceProvider.php file under App\Providers.
- Use the Schema class at top of the AppServiceProvider.php file.
- Assign defaultStringLength in the boot function.
- Save the file.
- Run migration again.
Code to fix Laravel Specified key was too long error
Step 1 – Add reference to Schema
use Illuminate\Support\Facades\Schema;
Step 2 – Add the following to the boot function of the document
Schema::defaultStringLength(191);
This should be enough to fix it.
2 thoughts on “Laravel 7.9: Specified key was too long error”
Pingback: How to create a custom 404 page in Laravel 7.x? | Rajiv Verma
Pingback: How to send notifications in Laravel 7.x? - Rajiv Verma