无法创建外键约束

时间:2022-05-17 04:36:52

I have a clients, feedback and a repairs tables. A client can give many feedbacks and have many repairs. In the feedback table I have created a clientid column (added also index) and I am able to create a foreign key to the clientid column (primary key) of the clients table.

我有客户,反馈和修理表。客户可以提供许多反馈和许多维修。在反馈表中,我创建了一个clientid列(也添加了索引),并且能够为clients表的clientid列(主键)创建一个外键。

The problem is that I am unable to do the same with the repairs table. Even though I have created a clientid column (indexed) within the repairs table and it has the same properties as the clientid within the clients table I get the following:

问题是我不能对修理台做同样的事情。尽管我在repair表中创建了一个clientid列(索引),它与clients表中的clientid具有相同的属性,但我得到了以下结果:

MySQL said: Documentation

MySQL说:文档

#1452 - Cannot add or update a child row: a foreign key constraint fails (ccsdb., CONSTRAINT #sql-3f0_8e5_ibfk_1 FOREIGN KEY (client_id) REFERENCES clients (client_id) ON DELETE CASCADE ON UPDATE CASCADE)

#1452 -不能添加或更新子行:外键约束失败(ccsdb)。,在更新级联的删除级联上,约束#sql-3f0_8e5_ibfk_1外键(client_id)引用客户端(client_id)

1 个解决方案

#1


0  

That error (Cannot add or update a child row: a foreign key constraint fails) is referenced in the MySQL FK Doc

这个错误(不能添加或更新子行:外键约束失败)在MySQL FK文档中被引用

To add a reference between 2 tables the condition must fit for existing data.

要在两个表之间添加引用,条件必须适合现有数据。

That means if you say table1.id = table2.id then all ids of table1 and table2 must match together.

这意味着如果你说表1。id =表二。然后,表1和表2的所有id必须匹配在一起。

To solve that you have to eliminate or fix those rows that don't match.
Example:

要解决这个问题,必须消除或修复那些不匹配的行。例子:

table1.id  |  table2.fk
   1       |       1      ok 
   2       |     null     error
   3       |       4      error if id 4 is not in table1

#1


0  

That error (Cannot add or update a child row: a foreign key constraint fails) is referenced in the MySQL FK Doc

这个错误(不能添加或更新子行:外键约束失败)在MySQL FK文档中被引用

To add a reference between 2 tables the condition must fit for existing data.

要在两个表之间添加引用,条件必须适合现有数据。

That means if you say table1.id = table2.id then all ids of table1 and table2 must match together.

这意味着如果你说表1。id =表二。然后,表1和表2的所有id必须匹配在一起。

To solve that you have to eliminate or fix those rows that don't match.
Example:

要解决这个问题,必须消除或修复那些不匹配的行。例子:

table1.id  |  table2.fk
   1       |       1      ok 
   2       |     null     error
   3       |       4      error if id 4 is not in table1