在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
我认为强制stringlength到191是一个非常糟糕的主意。
所以我去调查了解到底发生了什么。
我注意到这个消息错误:
SQLSTATE[42000]:语法错误或访问违规:1071指定的键
太长了;最大密钥长度为767字节
在我更新MySQL版本后开始出现。所以我已经用PHPMyAdmin检查了表,我注意到所有创建的新表都带有utf8mb4_unicode_ci,而不是旧表的utf8_unicode_ci。
在我的doctrine配置文件中,我注意到charset被设置为utf8mb4,但我之前的所有表都是在utf8中创建的,所以我猜这是一些更新魔术,它开始在utf8mb4上工作。
现在最简单的解决方法是更改ORM配置文件中的行字符集。
然后使用utf8mb4_unicode_ci删除表(如果您在dev模式下),或者如果您不能删除它们则修复字符集。
Symfony 4
在config/packages/doctrine.yaml中将字符集:utf8mb4修改为字符集:utf8
现在我的理论迁移又开始起作用了。
1-进入/config/database.php,找到这些行
'mysql' => [
...,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
...,
'engine' => null,
]
并更改为:
'mysql' => [
...,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
...,
'engine' => 'InnoDB',
]
2-运行php artisan config:cache重新配置laravel
3-删除数据库中现有的表,然后再次运行php artisan migrate
如前所述,我们在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工匠迁移