美文网首页
laravel 遇到的坑(1)

laravel 遇到的坑(1)

作者: shuolol | 来源:发表于2020-12-16 11:28 被阅读0次

Laravel 5.4对默认数据库字符集进行了更改,现在utf8mb4它包含了对存储表情符号的支持。这只会影响新的应用程序,只要您运行MySQL v5.7.7及更高版本,就不需要做任何事情。

对于运行MariaDB或旧版MySQL的用户,在尝试运行迁移时可能会遇到此错误:

[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]:语法错误或访问冲突:1071指定密钥太长; 最大密钥长度为767字节

您只需编辑 AppServiceProvider.php文件并在 boot方法内设置默认字符串长度:


use Illuminate\Support\Facades\Schema;
 
public function boot()
{
    Schema::defaultStringLength(191);
}

最大长度是767字节,utf8mb4编码每字符使用4字节,所以 767 / 4 = 191.75,所以将string的默认长度设置为191字符即可。

相关文章

网友评论

      本文标题:laravel 遇到的坑(1)

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