美文网首页程序员
Laravel 5.4 数据库迁移错误

Laravel 5.4 数据库迁移错误

作者: 本间麻衣子 | 来源:发表于2017-05-28 00:27 被阅读0次

当你执行 php artisan migrate 你会发现 surprise

why

Laravel 5.4改变了默认的数据库字符集,现在utf8mb4包括存储emojis支持。

错误

 [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`))

解决方案

  • 第一种
    编辑AppServiceProvider.php文件,设置默认字符串长度
use Illuminate\Support\Facades\Schema;
public function boot()
{
    Schema::defaultStringLength(191);
}
  • 第二种
    通常使用 mysql or mariadb 数据库才会出现这种问题,数据库版本应满足 ** mysql > 5.7.7 or mariadb > 10.2.2 **,升级数据库同时更改字符集编码
    我的数据库配置文件内容
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

这样一切都应该正常工作。

相关文章

网友评论

    本文标题:Laravel 5.4 数据库迁移错误

    本文链接:https://www.haomeiwen.com/subject/lwnrxxtx.html