Posts Learn Components Snippets Categories Tags Tools new About
/

How to Solve Laravel Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Learn how to How to Solve Laravel Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes the easy way.

Created on Oct 18, 2021

29 views

Sometimes you might come across an error that outputs "Laravel Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes" and to fix this error you have several options. Let's find out in these snippets.
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Option 1: Update default String Length


The first option is to change the default string length and you can define this in the AppServiceProvider. This is the most common method that should do the fix but if you still having error then do check the option 2 below.
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

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

Opton 2: Update Config/Database.php File


The next option is to update the "config/database.php" file for the "mysql array" configuration. Do update the value below for the "mysql" config and re-run the migration.
'mysql' => [
    'charset' => 'utf8',
    'collation' => 'utf8_general_ci',
    'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
]
The migration command should be run otherwise you can use the "migrate:fresh" command.
php artisan migrate

// or

php artisan migrate:fresh
If you have cached the configuration then do run the clear command first and then the migration command as mentioned above.
php artisan config:clear

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

)