Laravel migration Error – Specified key was too long

Laravel migration Error – Specified key was too long

As seen above in image, this issue you may have faced many a times. That’s why you’re on this page. This is a nasty error that says (1071 Specified key was too long) and your migration fails. You need to delete entry from migration table and also delete a table sometimes.

This is because of changes in Laravel 5.4, which uses default character set of database as utf8mb4. Which will allow to store emojis.

Those who are using MySQL 5.7.7 and higher do not need to do anything.

For those running older version of MySQL or MariaDB may have faced error like below:

[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

To fix this there’s one little change in AppServiceProvider.php file and you’re good.

Add below shown one line in boot method of this file. And import Schema before boot method.

use Illuminate\Support\Facades\Schema;
public function boot() {
     Schema::defaultStringLength(191);
}