Laravel Specified Key


Laravel 5.4: Specified key was too long error


The Laravel 5.4 made some changes to the database which limits the key length and when you migrate your database it sometimes shows an error that states: 

[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

As mentioned in their documentation here to fix this error all we have to do is to navigate to AppServiceProvider.php file and inside the boot method we set a default string length:

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

We also need to add Schema Facades to the top of AppServiceProvider file.

use Illuminate\Support\Facades\Schema;

After that Everything should work as normal.

No comments:

Post a Comment