我正在try 迁移Laravel中的用户表.当我运行迁移时,会出现以下错误:

[照明\数据库\查询异常]SQLSTATE[42000]:语法错误 或访问冲突:1071指定的密钥太长;最大密钥长度 为767字节(SQL:ALTER TABLE users ADD UNIQUE Users_Email_uniq(email))

我的迁移如下:

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');
});

在谷歌搜索之后,我发现了this bug report,泰勒说您可以将索引键指定为unique()的第二个参数,我已经这样做了.它仍然会给出错误.这里发生什么事情?

推荐答案

请为邮箱指定较小的长度:

$table->string('email', 250);

实际上,这是默认设置:

$table->string('email');

你应该表现得很好.

对于Laravel 5.4,你可以在这Laravel 5.4: Specified key was too long error, Laravel News篇文章中找到解决方案:

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Database\Schema\Builder;


public function boot()
{
    Builder::defaultStringLength(191);
}

Php相关问答推荐

文件大小在php下载读取文件中不可见

CodeIgniter AJAX表格提交中的CSRF token 再生问题

在Woocommerce变量产品中的标签附近显示所选属性选项名称

PHP日期操作,将多个日期输出为格式化字符串

Laveel Livewire Get Groupby姓名列表

使用MySQLi使用连接字符串设置字符集

根据WooCommerce中的客户计费国家/地区更改产品价格

如何在execute语句中使用存储过程参数?

在PHP中处理Linux输入事件(/dev/input/Event*)

如何在laravel中添加延迟?

即使在WooCommerce购物车中添加了两次产品,也要将所有项目设置为空白行

如何在 apache Laragon - laravel 10 中允许方法 DELETE?

Woocommerce 相关产品按其类别或排名数学主要类别

simplexml_load_file 和 simplexml_load_string 返回具有不同编码的相同数据

将Shopware 6后端中定义的值导入MultiFilter

Laravel的Storage::exists返回false,而file_exists返回true.

如何在 Laravel 中修改嵌套数组的键?

ACF 更新字段功能不更新 WordPress 中的任何数据

如何使用动态变量定义 bind_result?

docker |管道失败的 ubuntu 源列表