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:更改表
users
添加唯一 users_email_uniq(
[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');
});
经过一番谷歌搜索后,我遇到了此错误报告,泰勒说您可以将索引键指定为第二个我已经完成的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屋!