在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


当前回答

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

use Illuminate\Support\Facades\Schema;

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

其他回答

对于可能遇到这种情况的任何人,我的问题是我正在创建一个类型为字符串的列,并试图使它->unsigned()当我想让它是一个整数时。

腔:

添加到。env文件

DB_ENGINE=InnoDB

如果你没有任何数据已经分配到你的数据库执行以下操作:

进入app/Providers/ appserviceprovider .php并添加

使用说明\ \ ServiceProvider的支持;

在方法boot()内部;

模式:defaultStringLength (191);

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

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

如前所述,我们在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工匠迁移

需要使用以下命令创建数据库。

CREATE DATABASE database_name CHARACTER SET utf8 COLLATE utf8_general_ci;

然后运行以下命令迁移数据库表。

php artisan migrate