How To Fix Laravel Specified Key Was Too Long Error

Many of us hit the error “Specified key was too long error” while running the migration in Laravel. And that is because of the MariaDB or older version of MySQL.

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

You can solve this issue by defining a key length to 191. Now the question is where we have to define the key length to be 191?

You need to locate the “AppServiceProvider.php” under the app/Provider directory. Open this file and locate the boot() method and add the following codes to fix the Specified key was too long error.

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

We hope this small snippet will help you to resolve the Specified key was too long error. If you like this article then please follow us on Facebook and Twitter.

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.