在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
我只是把这个答案加在这里,因为这对我来说是最快的解决方法。只需要将默认的数据库引擎设置为'InnoDB'
/ config / database.php
'mysql' => [
...,
...,
'engine' => 'InnoDB',
]
然后运行PHP artisan config:cache来清除和刷新配置缓存
编辑:
这里找到的答案可能解释了这一事件背后的原因
如果您想在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条目。
推荐的解决方案是启用MySQL的innodb_large_prefix选项,这样你就不会陷入后续的问题。下面是如何做到这一点:
打开my.ini MySQL配置文件,并像这样在[mysqld]行下添加以下行。
[mysqld]
innodb_file_format = Barracuda
innodb_large_prefix = 1
innodb_file_per_table = ON
之后,保存更改并重新启动MySQL服务。
如果需要回滚,然后重新运行迁移。
如果您的问题仍然存在,请转到数据库配置文件并设置
'engine' => null, to 'engine' => 'innodb row_format=dynamic'
希望能有所帮助!
如果您运行“php artisan migrate”时出现此错误。你可以这样修改你想要更新的表:
DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');
在迁移脚本中。例子:
class MyMigration extends Migration {
/**
* Run the migrations.
*/
public function up()
{
DB::statement('ALTER TABLE table_name ROW_FORMAT = DYNAMIC;');
Schema::table('table_name', function ($table) {
//....
});
}
/**
* Undo the migrations.
*/
public function down()
{
//....
}
}
然后再次运行php artisan migrate