Fixing syntax error or access violation 1071 specified key was too long max key length is 767 bytes

Fixing syntax error or access violation 1071 specified key was too long max key length is 767 bytes

[IlluminateDatabaseQueryException]
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

Solution:-Remove all migrated tables first from database.

Then,In App/Providers/ folder open “AppServiceProvider.php” and add below codes.

Step 1. On add “use IlluminateSupportFacadesSchema;” under namespace AppProviders;.
Step 2. In boot method add \Schema::defaultStringLength(191);

<?php

namespace App\Providers;
use IlluminateSupportFacadesSchema;

use Illuminate\Support\ServiceProvider;

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

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


Leave a Reply

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