Laravel Migration: Unique Key Is Too Long, Even If Specified

Laravel migration: unique key is too long, even if specified

Specify a smaller length for your e-mail:

$table->string('email', 250);

Which is the default, actually:

$table->string('email');

And you should be good.

For Laravel 5.4 you can find a solution in this Laravel 5.4: Specified key was too long error, Laravel News post:

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Database\Schema\Builder;


public function boot()
{
Builder::defaultStringLength(191);
}

Laravel migration: unique key is too long, even if specified

Specify a smaller length for your e-mail:

$table->string('email', 250);

Which is the default, actually:

$table->string('email');

And you should be good.

For Laravel 5.4 you can find a solution in this Laravel 5.4: Specified key was too long error, Laravel News post:

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Database\Schema\Builder;


public function boot()
{
Builder::defaultStringLength(191);
}

Specified key was too long for unique keys: laravel migrations

As the error message implies, the unique key is too long for the DB.

You can name your unique indices in Laravel migrations like so:

Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('name');
$table->string('username');
$table->string('email');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->unique('username', 'username');
$table->unique('email', 'email');
});

This way your unique index for username will be named username instead of users_username_unique and so, will not exceed the byte limit for the index name

Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

According to the official Laravel 7.x documentation, you can solve this quite easily.

Update your /app/Providers/AppServiceProvider.php to contain:

use Illuminate\Support\Facades\Schema;

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

Alternatively, you may enable the innodb_large_prefix option for your database. Refer to your database's documentation for instructions on how to properly enable this option.

Laravel migration: unique key is too long, even if specified

Specify a smaller length for your e-mail:

$table->string('email', 250);

Which is the default, actually:

$table->string('email');

And you should be good.

For Laravel 5.4 you can find a solution in this Laravel 5.4: Specified key was too long error, Laravel News post:

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Database\Schema\Builder;


public function boot()
{
Builder::defaultStringLength(191);
}

Migration problem when database engine is set to MyISAM

That error has to do with the string length. By default, laravel's creates string columns with a length of 255 using the charset/collation utf8mb4 (4-byte UTF-8 Unicode Encoding).

Simply lower the default setting in your AppServiceProvider class. (app\Providers\AppServiceProvider.php)

use Illuminate\Support\Facades\Schema;

public function boot()
{
Schema::defaultStringLength(191); // I think you can use 250 or 249 instead
}

I think you can use 250 (250 * 4 = 1000 <= 1000) or 249 (249 * 4 = 996 < 1000) instead of 191.

The default, 255 (255 * 4 = 1020 > 1000) goes over the limit.

The reason the documentation uses 191 is because InnoDB's index limit is at 767 (191 * 4 = 764 < 767)


More information: 8.x - Migrations - Index Lengths & MySQL / MariaDB



Related Topics



Leave a reply



Submit