简体   繁体   English

1452 - 无法添加或更新子行:外键约束失败

[英]1452 - Cannot add or update a child row: a foreign key constraint fails

I'm getting this error: 我收到这个错误:

1452 - Cannot add or update a child row: a foreign key constraint fails. 1452 - 无法添加或更新子行:外键约束失败。

I've located them and try to get rid of the references with 我找到了他们并试图摆脱参考

alter table tillhör drop foreign key kat_id;

But getting this error instead: 但是反而得到这个错误:

#1025 - Error on rename of '.\\recept\\tillh@1ir' to '.\\recept#sql2-1570-3cb' (errno: 152). #1025 - 将'。\\ _ \\ _对于'。\\ _对于'。\\ _对于'。\\ _对于'。\\ recept#sql2-1570-3cb'(错误号:152)重命名。

What do I do wrong? 我做错了什么?

Before you query run 在查询运行之前

SET FOREIGN_KEY_CHECKS=0

then set it to 1 after you`re done. 完成后将其设置为1。

I face same problem. 我面临同样的问题。 I solve this issue by clearing, ie deleting all data from child table and successfully done. 我通过清除来解决这个问题,即从子表中删除所有数据并成功完成。

This is occur if child table contain some data with the foreign key that that are not in parent table ie, if there are two table called Person (with column id, name, address) and order (with column id, person_id, order_name); 如果子表包含一些不在父表中的外键的数据,即如果有两个名为Person (具有列id,名称,地址)和顺序 (具有列id,person_id,order_name)的表,则会发生这种情况; order.person_id is foreign key of person.id and order table contain person_id that is not present in person table . order.person_id是person.id的外键,order表包含person表中不存在的person_id

You can solve this using the following query 您可以使用以下查询解决此问题

Delete from order where person_id NOT IN (select id from person where person.id = order.person_id)

When I had this problem, it was due to the fact that I forgot to specify NULLS allowed when creating the foreign key id fields. 当我遇到这个问题时,原因是我在创建外键id字段时忘记指定NULLS。 I changed it after the fact, but 0's were already in the value. 之后我改变了它,但0已经在价值中。 It could not find 0 in the matching table, and then gave this error. 它在匹配表中找不到0,然后给出了这个错误。 The fix: update the zero values to nulls. 修复:将零值更新为空值。

您必须将空值设置为从父表到子表或者设置相同的值,例如Child table first nam = Derick Parent table name = Derick

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 1452无法添加或更新子行:外键约束失败 - 1452 Cannot add or update a child row: a foreign key constraint fails #1452-无法添加或更新子行:外键约束失败 - #1452 - Cannot add or update a child row: a foreign key constraint fails #1452-无法添加或更新子行:外键约束失败 - #1452 - Cannot add or update a child row: a foreign key constraint fails #1452 - 无法添加或更新子行:外键约束失败 - #1452 - Cannot add or update a child row: a foreign key constraint fails #1452-无法添加或更新子行:外键约束失败3 - #1452 - Cannot add or update a child row: a foreign key constraint fails 3 错误1452:1452:无法添加或更新子行:外键约束失败 - ERROR 1452: 1452: Cannot add or update a child row: a foreign key constraint fails 错误:无法添加外键关系:#1452 - 无法添加或更新子行:外键约束失败 - Error: FOREIGN KEY relationship could not be added! #1452 - Cannot add or update a child row: a foreign key constraint fails SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败 - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 违反完整性约束:1452无法添加或更新子行:外键约束失败 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 违反完整性约束:1452无法添加或更新子行:外键约束失败 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM