在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(也适用于8X):简单的解决方案。
选项1:
php artisan db:wipe
更新/config/database.php中mysql数组的这些值
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
然后
php artisan migrate
这是完成了!迁移表创建成功。
选项2:
使用php artisan db:手动擦除或删除数据库中的所有表。
更新AppServiceProvider.php[位于app/Providers/AppServiceProvider.php]
use Illuminate\Support\Facades\Schema;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
然后
php artisan migrate
这是完成了!
陷阱:我想提一下@shock_gone_wild的评论
注意这个解决方案(选项2)。例如,如果您索引电子邮件字段,
存储邮件的最大长度为191个字符。这个更少
比RFC官方声明的要多。
可选地,我尝试了这些可能的方法(如下面),但不工作。
PHP artisan config:cache PHP artisan migrate:fresh
PHP工匠迁移:重置
如果您想在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条目。
如前所述,我们在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工匠迁移