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

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

1.9K

2 years ago

0 comments

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
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this