SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes


Hi friends, in this tutorial, you will learn how to resolve SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table users add unique users_email_unique(email)). This error occurs when we run the database migrations in any of the Laravel projects with the help of the artisan command php artisan make: migrate as shown below.

1071 Specified key was too long

In the above image, you have noticed that the error occurs after running the migration artisan command. But don’t worry, if you are wondering to fix this then you have come to the right place. This particular error is shown at the time of migrating tables that are defined under your Laravel project/database/migrations.

These kinds of errors often occur due to the structure declaration in the MySQL databases such as index column size too large and row size too large etc. You can fix these errors in phpmyadmin of your local server like the Wamp server or xampp server etc.

Also read, How to fix phpmyadmin #1709 – index column size too large

Steps to fix SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

Step 1:- Go to AppServiceProvider of your project under app/Providers/AppServiceProvider.

Step 2:- Now, open the file and add the below line after the namespace.

use Illuminate\Support\Facades\Schema;

Step 3:- Paste the below code inside the boot() method.

Schema::defaultStringLength(191);

AppServiceProvider.php

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
            Schema::defaultStringLength(191);
    }
}

Step 4:- Now, again run the artisan command as given below.

1071 Specified key was too long

Now, you can see that all the tables are migrated successfully from the database/migrations from your Laravel project.

Also read, How to resolve #1118 – Row size too large (> 8126)

Conclusion:- I hope this tutorial will help you to resolve the issue. If there is any doubt then please leave a comment below.


Leave a Comment