Laravel迁移错误:语法错误或访问冲突:1071指定的密钥太长;最大密钥长度为767字节 [英] Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
问题描述
使用php artisan make:auth
Migration error on Laravel 5.4 with php artisan make:auth
[Illuminate \ Database \ QueryException] SQLSTATE [42000]:语法错误或访问冲突:1071指定的密钥太长;已更改.最大密钥长度为767字节(SQL:alter tabl e
users
添加唯一的users_email_unique
(
[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 uniqueusers_email_unique
(
[PDOException] SQLSTATE [42000]:语法错误或访问冲突:1071指定的密钥太长;默认值为0.最大密钥长度为767字节
[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
推荐答案
根据官方文档,您可以轻松解决此问题.
According to the official documentation, you can solve this quite easily.
将以下代码添加到 AppServiceProvider.php (/app/Providers/AppServiceProvider.php)
Add following code to AppServiceProvider.php (/app/Providers/AppServiceProvider.php)
use Illuminate\Database\Schema\Builder; // Import Builder where defaultStringLength method is defined
function boot()
{
Builder::defaultStringLength(191); // Update defaultStringLength
}
MySQL始终将UTF8字段的最大数量保留为4个字节,因此使用DEFAULT CHARACTER SET 255 + 255时为utf8mb4 COLLATE utf8mb4_unicode_ci;您超过了767个最大密钥长度限制.通过@scaisedge
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
这篇关于Laravel迁移错误:语法错误或访问冲突:1071指定的密钥太长;最大密钥长度为767字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!