在Laravel 5.4上使用php artisan make:auth迁移错误

[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 unique users_email_unique(email)) [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes


当前回答

你可以这样设置索引字段的字符串长度:

  $table->string('email', 200)->unique();

其他回答

最干净的解决方案是去你的数据库并更改:

default_storage_engine to InnoDB

你很可能有MyISAM。

我只是把这个答案加在这里,因为这对我来说是最快的解决方法。只需要将默认的数据库引擎设置为'InnoDB'

/ config / database.php

'mysql' => [
    ...,
    ...,
    'engine' => 'InnoDB',
 ]

然后运行PHP artisan config:cache来清除和刷新配置缓存

编辑: 这里找到的答案可能解释了这一事件背后的原因

你可以这样设置索引字段的字符串长度:

  $table->string('email', 200)->unique();

我得到了这个错误尽管我已经有了(实际上因为我已经有了) 模式:defaultStringLength (191);在AppServiceProvider.php文件中。

原因是我试图在我的一个迁移中设置一个高于191的字符串值:

Schema::create('order_items', function (Blueprint $table) {
    $table->primary(['order_id', 'product_id', 'attributes']);
    $table->unsignedBigInteger('order_id');
    $table->unsignedBigInteger('product_id');
    $table->string('attributes', 1000); // This line right here
    $table->timestamps();
});

删除1000或将其设置为191解决了我的问题。

没有人告诉的解决方案是,在Mysql v5.5和以后的InnoDB是默认的存储引擎,它没有这个问题,但在许多情况下,像我的,有一些旧的Mysql ini配置文件使用旧的MYISAM存储引擎,如下所示。

default-storage-engine=MYISAM

这造成了所有这些问题,解决方案是将Mysql的ini配置文件中的默认存储引擎一劳永逸地更改为InnoDB,而不是做临时的hack。

default-storage-engine=InnoDB

如果你在MySql v5.5或更高版本,那么InnoDB是默认引擎,所以你不需要像上面那样显式地设置它,只要从你的ini文件中删除default-storage-engine=MYISAM(如果它存在),你就可以开始了。