IT & Programming

Laravel SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long

You this error while running first migration command ater installing fresh copy of Laravel 8.x. It is strange that Laravel developer do not know about this isssue.

Below are 3 ways to fix this issue.

Solution 1:

Open config/database.php file and add 'InnoDB' to 'engine' key for 'mysql' connection array. The line number should be 60 in original database.php file.

'engine' => 'InnoDB',

Solution 2:

Open app/Providers/AppServiceProvider.php and add a line of code to boot() method as shown below.

use Illuminate\Support\Facades\Schema;

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

Solution 3:

Open config/database.php file and set 'charset' to 'utf8' and 'collation' to 'utf8_unicode_ci'. The line numbesr should be 55 and 56 in original database.php file.

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

Solution 4:

In your migration, give smaller length to your string type field.

$table->string('email', 128)->unique();

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *