Laravel Migration Error: Syntax error or access vi

2019-01-04 23:18发布

Migration error on Laravel 5.4 with php artisan make:auth

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter tabl e users add unique users_email_unique(email))

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

23条回答
Animai°情兽
2楼-- · 2019-01-04 23:37

As already specified we add to the AppServiceProvider.php in App/Providers

use Illuminate\Support\Facades\Schema;  // add this

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

you can see more details in the link bellow (search for "Index Lengths & MySQL / MariaDB") https://laravel.com/docs/5.5/migrations

BUT WELL THAT's not what I published all about! the thing is even when doing the above you will likely to get another error (that's when you run php artisan migrate command and because of the problem of the length, the operation will likely stuck in the middle. solution is below, and the user table is likely created without the rest or not totally correctly) we need to roll back. the default roll back will not work. because the operation of migration didn't like finish. you need to delete the new created tables in the database manually.

we can do it using tinker as in below:

L:\todos> php artisan tinker

Psy Shell v0.8.15 (PHP 7.1.10 — cli) by Justin Hileman

>>> Schema::drop('users')

=> null

I myself had a problem with users table.

after that you're good to go

php artisan migrate:rollback

php artisan migrate

查看更多
疯言疯语
3楼-- · 2019-01-04 23:37

For me what worked was to update the dependencies by running.

composer update

You should also install the latest mysql version.

查看更多
Juvenile、少年°
4楼-- · 2019-01-04 23:38

In the AppServiceProvider.php,you include this code top of the file.

use Illuminate\Support\Facades\Schema;

And you add this code in boot method.

 Schema::defaultStringLength(191);
查看更多
来,给爷笑一个
5楼-- · 2019-01-04 23:39

I'm just adding this answer here as it's the quickest solution for me. Just set the default database engine to 'InnoDB' on

/config/database.php

'mysql' => [
    ...,
    ...,
    'engine' => 'InnoDB',
 ]

then run php artisan config:cache to clear and refresh the configuration cache

查看更多
淡お忘
6楼-- · 2019-01-04 23:41

I have just modified following line in users and password_resets migration file.

Old : $table->string('email')->unique();

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

Voila!!

查看更多
在下西门庆
7楼-- · 2019-01-04 23:42

The issue solved by the following way.

Add the following code in AppServiceProvider.php

L5_root/app/Providers/AppServiceProvider.php

The code block

use Illuminate\Support\Facades\Schema; //Import Schema

function boot()
{
    Schema::defaultStringLength(191); //Solved by increasing StringLength
}

MySQL reserves always the max amount for a UTF8 field which is 4 bytes so with 255 + 255 with your DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; you are over the 767 max key length limit. By @scaisedge

查看更多
登录 后发表回答