Laravel 5.4: Specified key was too long error

2,423

Laravel 5.4 and higher uses utf8mb4 character set by default, which also includes support for storing emojis. It only affects new applications and as long as you are running MySQL higher than 5.7.7 release or MariaDB higher than 10.2.2 release.

If you are running an older version of MySQL or MariaDB, you will get this error when you are trying to run migrations.

[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

It is outlined in Laravel Migration Guide. To fix this, you need to manually configure the default string length generated by migrations. Edit your AppServiceProvider.php file located in app/Providers/AppServiceProvider.php and edit your boot method to set default string length. Also, include this use statement use Illuminate\Support\Facades\Schema; as well.

use Illuminate\Support\Facades\Schema;

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

Now, everything will work fine.

You might also like
Comments