在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();

其他回答

为了避免更改代码中的任何内容,只需将MySQL服务器更新到至少5.7.7

更多信息请参考:https://laravel-news.com/laravel-5-4-key-too-long-error

正如迁移指南中所概述的那样,你所要做的就是编辑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

这很常见,因为Laravel 5.4将默认数据库字符集更改为utf8mb4。你要做的是:编辑你的App\Providers.php,把这段代码放在类声明之前

use Illuminate\Support\Facades\Schema;

另外,将此添加到'boot'功能 模式:defaultStringLength (191);

在database.php

-添加这一行:

引擎=>无害的ROW_FORMAT=动力,