Laravel 迁移:唯一键太长,即使指定 [英] Laravel migration: unique key is too long, even if specified
问题描述
我正在尝试迁移 Laravel 中的用户表.当我运行迁移时,出现此错误:
I am trying to migrate a users table in Laravel. When I run my migration I get this error:
[Illuminate\Database\QueryException] SQLSTATE[42000]:语法错误或访问冲突:1071 指定的密钥太长;最大密钥长度是 767 字节(SQL:alter table users
add unique table users
users_email_uniq(email
))
[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_uniq(
我的迁移如下:
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name', 32);
$table->string('username', 32);
$table->string('email', 320);
$table->string('password', 64);
$table->string('role', 32);
$table->string('confirmation_code');
$table->boolean('confirmed')->default(true);
$table->timestamps();
$table->unique('email', 'users_email_uniq');
});
经过一番谷歌搜索后,我发现了此错误报告,其中 Taylor 说您可以将索引键指定为第二个unique()
的参数,我已经完成了.它仍然给出错误.这是怎么回事?
After some googling I came across this bug report where Taylor says you can specify the index key as the 2nd parameter of unique()
, which I have done. It still gives the error. What is going on here?
推荐答案
为您的电子邮件指定更小的长度:
Specify a smaller length for your e-mail:
$table->string('email', 250);
实际上是默认值:
$table->string('email');
你应该很好.
对于 Laravel 5.4,您可以在此 Laravel 5.4 中找到解决方案: 指定键太长错误,Laravel 新闻 发帖:
For Laravel 5.4 you can find a solution in this Laravel 5.4: Specified key was too long error, Laravel News post:
如迁移指南中所述,您只需编辑 AppServiceProvider.php 文件并在启动方法中设置默认字符串长度即可解决此问题:
use Illuminate\Database\Schema\Builder;
public function boot()
{
Builder::defaultStringLength(191);
}
这篇关于Laravel 迁移:唯一键太长,即使指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!