在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


当前回答

在database.php

-添加这一行:

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

其他回答

Laravel 5.4由数据库版本导致。

根据文档(在索引长度和MySQL / MariaDB部分):

Laravel默认使用utf8mb4字符集,其中包括 支持在数据库中存储“表情包”。如果你正在运行一个 MySQL版本高于5.7.7或MariaDB版本高于5.7.7 对于10.2.2版本,您可能需要手动配置默认值 由迁移生成的字符串长度,以便MySQL创建 它们的索引。方法进行配置 方案::defaultStringLength方法在你的AppServiceProvider。

换句话说,在<ROOT>/app/Providers/AppServiceProvider.php:

// Import Schema
use Illuminate\Support\Facades\Schema;
// ...

class AppServiceProvider extends ServiceProvider
{

public function boot()
{
    // Add the following line
    Schema::defaultStringLength(191);
}

// ...

}

但正如另一个答案的评论所说:

注意这个解决方案。例如,如果您索引电子邮件字段, 存储邮件的最大长度为191个字符。这个更少 比RFC官方声明的要多。

因此,文档还提出了另一种解决方案:

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

我添加了两个对我有用的解决方案。

第一个解决方案是:

在config目录下打开database.php文件。 将'engine' =>修改为'engine' => 'InnoDB' 这对我很管用。

第二个解决方案是:

在config目录下打开database.php文件。 2.编辑'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 来 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci',

古德勒克

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

/ config / database.php

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

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

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

模式: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中进行更改,那么您需要在迁移中定义电子邮件字段的长度。只需将第一行代码替换为第二行。

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条目。