在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


当前回答

我刚刚修改了users和password_reset迁移文件中的以下行。

旧:$table->string('email')->unique();

新:$table->string('email', 128)->unique();

其他回答

据官方Laravel 7日报道。X文档,可以很容易地解决这个问题。

更新你的/app/Providers/AppServiceProvider.php包含:

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

或者,您可以为您的数据库启用innodb_large_prefix选项。有关如何正确启用此选项的说明,请参阅数据库的文档。

如果您想在AppServiceProvider中进行更改,那么您需要在迁移中定义电子邮件字段的长度。只需将第一行代码替换为第二行。

create_users_table

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

create_password_resets_table

$table->string('email')->index();
$table->string('email', 50)->index();

在成功更改之后,您可以运行迁移。 注意:首先你必须从数据库中删除(如果你有)users表,password_resets表,并从迁移表中删除users和password_resets条目。

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

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

对我来说很有魅力!

将此添加到config/database.php

'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',

而不是

'engine' => 'null',

打开这个文件:/app/Providers/AppServiceProvider.php

并将此代码更新为我的图像:

use Illuminate\Support\Facades\Schema;

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