在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


当前回答

只要在/etc/mysql/mariadb.conf.d/50-server.cnf或其他配置文件中写几行:

innodb-file-format=barracuda
innodb-file-per-table=ON
innodb-large-prefix=ON
innodb_default_row_format = 'DYNAMIC'

和sudo服务mysql重启

https://stackoverflow.com/a/57465235/778234

查看文档:https://dev.mysql.com/doc/refman/5.6/en/innodb-row-format.html

其他回答

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

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

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

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

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

模式:defaultStringLength (191);将默认定义所有字符串191的长度,这可能会破坏您的数据库。你不能走这条路。

只需在数据库迁移类中定义任何特定列的长度。例如,我在CreateUsersTable类中定义了“name”,“username”和“email”,如下所示:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 191);
            $table->string('username', 30)->unique();
            $table->string('email', 191)->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

对于不想更改AppServiceProvider.php的人。 (在我看来,仅仅为了迁移而更改AppServiceProvider.php是一个坏主意)

您可以将数据长度添加回database/migrations/下的迁移文件,如下所示:

create_users_table.php

$table->string('name',64);
$table->string('email',128)->unique();

create_password_resets_table.php

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

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

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