Laravel migration Error – Specified key was too long

image_2019_03_18T18_00_51_644Z.png

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);
}

Shyam has written 29 articles

Shyam is senior full stack developer, who loves to explore new technologies and work on them. He's passionate about coding so can code 24/7. He uses PHP as a backend programming language.

He knows Laravel, MySQL, AngularJS, ReactJS, Redis, Kubernetes, Git, CodeIgniter, PHP, MVC pattern, Lodash, jQuery, VanilaJS, Teamcity and many other technologies and tools.

Shyam writes notes and hacks on his blog (https://shyammakwana.me). In spare time he can be found @ StackOverflow or crafting any new open source application.

Passionate Programmer and Meditator #PERIOD.