在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;  // add this

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

你可以在下面的链接中看到更多细节(搜索“索引长度& MySQL / MariaDB”) https://laravel.com/docs/5.5/migrations

BUT WELL THAT's not what I published all about! the thing is even when doing the above you will likely to get another error (that's when you run php artisan migrate command and because of the problem of the length, the operation will likely stuck in the middle. solution is below, and the user table is likely created without the rest or not totally correctly) we need to roll back. the default roll back will not work. because the operation of migration didn't like finish. you need to delete the new created tables in the database manually.

我们可以这样做使用修补器如下所示:

L:\todos> php artisan tinker

Psy Shell v0.8.15 (PHP 7.1.10 — cli) by Justin Hileman

>>> Schema::drop('users')

=> null

我自己有一个问题与用户表。

之后你就可以开始了

PHP artisan migrate:回滚

PHP工匠迁移

其他回答

对于不想更改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并添加

使用说明\ \ ServiceProvider的支持;

在方法boot()内部;

模式:defaultStringLength (191);

现在删除数据库中的记录,用户表中的ex。 运行以下命令

PHP工匠配置:缓存 PHP工匠迁移

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

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

use Illuminate\Support\Facades\Schema;

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

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

最干净的解决方案是去你的数据库并更改:

default_storage_engine to InnoDB

你很可能有MyISAM。