在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


当前回答

你可以这样设置索引字段的字符串长度:

  $table->string('email', 200)->unique();

其他回答

只要在/etc/mysql/mariadb.conf.d/50-server.cnf或其他配置文件中写几行:

innodb-file-format=barracuda
innodb-file-per-table=ON
innodb-large-prefix=ON
innodb_default_row_format = 'DYNAMIC'

和sudo服务mysql重启

https://stackoverflow.com/a/57465235/778234

查看文档:https://dev.mysql.com/doc/refman/5.6/en/innodb-row-format.html

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

CREATE DATABASE database_name CHARACTER SET utf8 COLLATE utf8_general_ci;

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

php artisan migrate

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工匠迁移:重置

与其设置长度限制,我建议以下方法,这对我来说很有效。

内部:

配置/ database.php

将这一行替换为mysql:

'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',

:

'engine' => null,

我刚刚修改了users和password_reset迁移文件中的以下行。

旧:$table->string('email')->unique();

新:$table->string('email', 128)->unique();