Laravel 5.6: Specified key was too long error

Laravel 5.6: Specified key was too long error

在Laravel执行以下命令:

php artisan migrate

这是由于Laravel5.6设置了数据库默认编码,现在的编码默认为“utf8mb4”(支持emoji),

开始查看了官网是这么说的:

“applications and as long as you are running MySQL v5.7.7 and higher you do not need to do anything.”

如果MySQL版本大于5.7.7就可以了,发现不行……

还是报:

[IlluminateDatabaseQueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table 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

所以还是按照Migrations的文档老老实实的修改吧:

打开 AppServiceProvider.php 文件写入如下:

use IlluminateSupportFacadesSchema;

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

在重新执行

php artisan migrate

成功!!!

原文地址:https://www.cnblogs.com/wilburxu/p/9355499.html