Php Laravel 5.1外键迁移错误
我在执行约束迁移时遇到了一个奇怪的错误 当我执行这个迁移时,我得到Php Laravel 5.1外键迁移错误,php,mysql,laravel,migration,laravel-5.1,Php,Mysql,Laravel,Migration,Laravel 5.1,我在执行约束迁移时遇到了一个奇怪的错误 当我执行这个迁移时,我得到 [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table 'flowtime_dev.#sql-85bb_52' (errno: 121) (SQL: alter table `questions` add constraint questions_inventory_i d_foreign
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'flowtime_dev.#sql-85bb_52' (errno: 121) (SQL: alter table `questions` add constraint questions_inventory_i
d_foreign foreign key (`inventory_id`) references `inventories` (`id`) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'flowtime_dev.#sql-85bb_52' (errno: 121)
我已经读到不同表中的数据类型应该匹配(比如int(10)到int(10)),它们是匹配的,这也是我所做的最后一次迁移(所有表都存在,并且以前都是由其他迁移创建的)
我真的不知道该怎么办了有人能帮忙吗?
我也看到过类似的帖子,但到目前为止他们还没有为我解决这个问题
迁移情况如下:
public function up()
{
Schema::table('questions', function ($table) {
$table->integer('inventory_id')->unsigned()->change();
$table->foreign('inventory_id')
->references('id')->on('inventories')
->onDelete('cascade');
});
Schema::table('question_response', function ($table) {
$table->integer('question_id')->unsigned()->change();
$table->foreign('question_id')
->references('id')->on('questions')
->onDelete('cascade');
});
Schema::table('question_response', function ($table) {
$table->integer('sample_result_set_id')->unsigned()->change();
$table->foreign('sample_result_set_id')
->references('id')->on('sample_response_set')
->onDelete('cascade');
});
Schema::table('inventory_response', function ($table) {
$table->integer('inventory_id')->unsigned()->change();
$table->foreign('inventory_id')
->references('id')->on('inventories')
->onDelete('cascade');
});
Schema::table('inventory_response', function ($table) {
$table->integer('sample_result_set_id')->unsigned()->change();
$table->foreign('sample_result_set_id')
->references('id')->on('sample_response_set')
->onDelete('cascade');
});
}
在迁移开始时,关闭外键约束
SET FOREIGN_KEY_CHECKS=0;
然后,在迁移数据时,将其重新启用
SET FOREIGN_KEY_CHECKS=1;
如果这不起作用,那么尝试添加以下行
$table->engine = 'InnoDB'
尝试使用两个单独的迁移 创建第一次迁移,它将创建表和所有列(将除
$table->foreign
子句以外的所有内容放在此处)。运行migrate
命令。不要执行第二次迁移,如果已经进行了此迁移,请将其临时从migrations目录中删除
然后创建第二个迁移,它将向已创建的列添加外键(此处仅使用$table->foreign
子句)。再次运行migrate
命令
当我处于同样的情况时,这对我很有帮助。我希望它也能对你起作用。你在使用哪个数据库?@AaronFranco MySqladded DB::statement('SET-FOREIGN\u-KEY\u-CHECKS=0');before和DB::statement('SET-FOREIGN\u-KEY\u-CHECKS=1'));迁移结束后@aaronfrancondb没有帮助aswll@aaronfrancondb主要问题是我很笨,有些迁移已经完成了,这意味着每次都失败了,我花了一些时间才意识到。谢谢你的帮助:)
$table->foreign('inventory_id', 'fk_questions_inventory_id')->...