Laravel 5.6: Specified key was too long error

Posted WilburXu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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就可以了,发现不行……

还是报:

[Illuminate\\Database\\QueryException]
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 Illuminate\\Support\\Facades\\Schema;

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

在重新执行

php artisan migrate

成功!!!

 

以上是关于Laravel 5.6: Specified key was too long error的主要内容,如果未能解决你的问题,请参考以下文章

Laravel出现Noapplicationencryptionkeyhasbeenspecified. (版本5.6.0)

解决Laravel错误1071 Specified key was too long的多种方法

Laravel 5 migrate时报错: Specified key was too long error

Laravel 出现 No application encryption key has been specified.

Laravel 5.4 migrate时报错: Specified key was too long error

Laravel 出现 No application encryption key has been specified.