在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


当前回答

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

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

use Illuminate\Support\Facades\Schema;

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

其他回答

如果您想在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条目。

据官方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选项。有关如何正确启用此选项的说明,请参阅数据库的文档。

正如迁移指南中所概述的那样,你所要做的就是编辑app/Providers/AppServiceProvider.php文件,并在boot方法中设置默认字符串长度:

use Illuminate\Support\Facades\Schema;

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

注意:首先你必须从数据库中删除(如果你有)users表,password_resets表,并从迁移表中删除users和password_resets条目。

要运行所有未完成的迁移,执行migrate Artisan命令:

php artisan migrate

在那之后,一切都应该正常工作。

将我的本地数据库服务器类型从“mariadb”更改为“mysql”,无需编辑任何Laravel文件就可以解决这个问题。

我按照本教程更改了我的db服务器类型:https://odan.github.io/2017/08/13/xampp-replacing-mariadb-with-mysql.html

这里的方法是传递一个带有键名的第二个参数(一个短参数):

$table->string('my_field_name')->unique(null,'key_name');