How to Handle Specified key was too long error during Laravel Database migration

In this Post i give solution for “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))” error during Laravel (5.4 or higher Versions) DB Migration.

Laravel 5.4 or Latest versions are made a change to the default database character set to utf8mb4, which includes support for storing emojis in db. if you are running your application in Lower than MySQL v5.7.7 or Lower than MariaDB  10.2.2 you may hit this error when trying to run db migrations.

How to Avoid It :

Upgrade your MySQL as higher than 5.7.7 version or MariaDB higher than 10.2.2 release.

How to Solve It :

To fix this Error, you need to manually configure the default string length generated by migrations. Edit your app/Providers/AppServiceProvider.php file 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 see your works Result…

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *