Laravel 迁移 - 违反完整性约束:1452 无法添加或更新子行:外键约束失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47266089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel migration - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
提问by Leff
I am trying to run a migration for a table inventories
that I have created with this migration:
我正在尝试为inventories
使用此迁移创建的表运行迁移:
Schema::create('inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('remote_id')->unsigned();
$table->integer('local_id')->unsigned();
$table->string('local_type');
$table->string('url')->nullable()->unique();
$table->timestamps();
});
I am trying to add a run a migration where I am adding a foreign key to the table:
我正在尝试添加运行迁移,在其中向表添加外键:
Schema::table('inventories', function (Blueprint $table) {
$table->foreign('local_id')->references('id')->on('contents')->onDelete('cascade');
});
But, I am getting an error when I try to run the migration:
但是,当我尝试运行迁移时出现错误:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
middleton
.#sql-5d6_162a
, CONSTRAINTinventories_local_id_foreign
FOREIGN KEY (local_id
) REFERENCEScontents
(id
) ON DELETE CASCADE ) (SQL: alter tableinventories
add constraintinventories_local_id_foreign
foreign key (local_id
) referencescontents
(id
) on delete cascade)[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
middleton
.#sql-5d6_162a
, CONSTRAINTinventories_local_id_foreign
FOREIGN KEY (local_id
) REFERENCEScontents
(id
) ON DELETE CASCADE )
[照明\数据库\查询异常]
SQLSTATE[23000]: 完整性约束违规:1452 无法添加或更新子行:外键约束失败 (
middleton
.#sql-5d6_162a
, CONSTRAINTinventories_local_id_foreign
FOREIGN KEY (local_id
) REFERENCEScontents
(id
) ON DELETE CASCADE ) (SQL: 更改表inventories
添加约束inventories_local_id_foreign
外键 (local_id
) 引用contents
(id
) 在删除级联上)[PDO 异常]
SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(
middleton
.#sql-5d6_162a
, CONSTRAINTinventories_local_id_foreign
FOREIGN KEY (local_id
) REFERENCEScontents
(id
) ON DELETE CASCADE )
What am I doing wrong?
我究竟做错了什么?
采纳答案by Darshan Mehta
You probably have some records in the inventories
table with local_id
that does not have corresponding id
in the contents
table, hence the error. You could solve it by one of the two ways:
您可能在inventories
表中有一些记录,但在表local_id
中没有对应id
的记录contents
,因此出现错误。您可以通过以下两种方式之一解决:
- Run the migration with
foreign_key_checks
turned off. This will disabled the foreign key constraints for the existing rows (if that's what you want). It's documented here - Insert only those records that have corresponding
id
field incontents
table. You can useINSERT INTO.. WHERE EXISTS
query to filter the records out, and insert only those records.
- 在
foreign_key_checks
关闭的情况下运行迁移。这将禁用现有行的外键约束(如果这是您想要的)。它记录在这里 - 只插入那些
id
在contents
表中具有相应字段的记录。您可以使用INSERT INTO.. WHERE EXISTS
查询来过滤记录,并仅插入这些记录。
回答by Roman Bobrik
had the same problem. fixed it by adding nullable
to field
有同样的问题。通过添加nullable
到字段来修复它
Schema::create('table_name', function (Blueprint $table) {
...
$table->integer('some_id')->unsigned()->nullable();
$table->foreign('some_id')->references('id')->on('other_table');
...
});
note that after migration all existed rows will have some_id = NULL
请注意,迁移后所有存在的行都将具有 some_id = NULL