在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 7日报道。X文档,可以很容易地解决这个问题。

更新你的/app/Providers/AppServiceProvider.php包含:

use Illuminate\Support\Facades\Schema;

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

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

其他回答

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

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

使用说明\ \ ServiceProvider的支持;

在方法boot()内部;

模式:defaultStringLength (191);

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

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

我不知道为什么上面的解和官方的解是相加的

Schema::defaultStringLength(191);

在AppServiceProvider中不适合我。 工作的是编辑config文件夹中的database.php文件。 只是编辑

'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',

to

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',

它应该可以工作,尽管你将无法存储扩展的多字节字符,如表情符号。

这是一个丑陋的黑客,如果你想存储字符串在非英语语言,表情符号

我用Laravel 5.7做的。

不要忘记停止并再次启动服务器。

尝试使用默认字符串长度125 (MySQL 8.0)。

defaultStringLength (125)

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

/ config / database.php

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

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

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

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