简体   繁体   English

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

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

NOTE: Before marking it as Duplicate, i want to tell you guys that i've gone through all these related problems in stackoverflow and other websites, but i didn't get any suitable solution, so that's why i am here for the help.注意:在将其标记为重复之前,我想告诉你们,我已经在 stackoverflow 和其他网站上解决了所有这些相关问题,但我没有找到任何合适的解决方案,所以这就是我来这里寻求帮助的原因。

I have two tables: employees and fines我有两个表:员工罚款

In fines table fine_id is the PK and employee_id is the FK references to employees table PK employee_id .fines表中fine_idPKemployee_id是对员工PK employee_idFK引用。

Now, i am creating fine_id (column in table employees ) as a FK reference to fine_id in fines table.现在,我正在创建fine_id (表employees中的列)作为对fines表中fine_idFK引用。

i am adding constraints via XAMPP PHPMYADMIN it auto generates my query which is:我通过XAMPP PHPMYADMIN添加约束,它会自动生成我的查询,即:

ALTER TABLE employees ADD CONSTRAINT fine_fk FOREIGN KEY (fine_id) REFERENCES fines(fine_id) ON DELETE CASCADE ON UPDATE CASCADE;

But, I am getting this error while running this SQL Query但是,我在运行此SQL Query时收到此错误

1452 - Cannot add or update a child row: a foreign key constraint fails ( ems_db . #sql-dd4_35 , CONSTRAINT fine_fk FOREIGN KEY ( fine_id ) REFERENCES fines ( fine_id ) ON DELETE CASCADE ON UPDATE CASCADE) 1452 - 无法添加或更新子行:外键约束失败( ems_db#sql-dd4_35 ,CONSTRAINT fine_fk FOREIGN KEY ( fine_id ) REFERENCES fines ( fine_id ) ON DELETE CASCADE ON UPDATE CASCADE)

Here are attached images:这里附上图片:

employees table员工

在此处输入图像描述

fines table罚款

在此处输入图像描述

error错误

在此处输入图像描述

Thanks.谢谢。

somehow i managed to solve my issue myself,我设法自己解决了我的问题,

the problem was that in my employees table i have the column fine_id which i want to make a FK , that column value was empty in my existing record in employees table, although i have values in my fines table, but the column fine_id in employees table was not filled, with the corresponding fine_id of fines table, so before creating FK i have filled my column with the correct ID's of fines table, and my problem was gone away.问题是在我的employees表中我有fine_id列,我想创建一个FK ,该列值在我现有的employees表记录中为空,虽然我的fines表中有值,但employees表中的fine_id列没有用相应的罚款fine_id填充,所以在创建FK之前,我已经用正确的罚款ID填充了我的列,我的问题就消失了。

I will suggest to use query to set foreign key.我会建议使用查询来设置外键。 Not graphically.不是图形化的。 That worked for me!这对我有用!

暂无
暂无

声明:本站的技术帖子网页,遵循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-无法添加或更新子行:外键约束失败3 - #1452 - Cannot add or update a child row: a foreign key constraint fails 3 1452 - 无法添加或更新子行:外键约束失败 - 1452 - Cannot add or update a child row: a foreign key constraint fails 错误1452:1452:无法添加或更新子行:外键约束失败 - ERROR 1452: 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 Laravel 5:违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM