Laravel: Syntax error or access Violation: 1071 Specified key was Too long

Asked

Viewed 862 times

0

I went to create the tables in the database, but shows me the error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes.

How to fix this?

2 answers

2


This happens because the Laravel changed the value default fields. To fix this, just go in the file app/Providers/AppServiceProvider.php and add the following code:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
  • In reality you are changing the default value by running this command, the error actually occurs because the database is not with the correct Match, in your database.php file you can change the charset and the collation and it will work, then you don’t need to touch the String pattern. 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',

0

Another way to solve is to add the index name in the second parameter:

$table->string('seu_campo')->unique(null,'chave');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.