SQL错误:1452:无法添加或更新子行:外键约束失败 [英] SQL Error: 1452: Cannot add or update a child row: a foreign key constraint fails
问题描述
我的数据库中有两个表:
-
order
li>
-
课程
。
order
有一个列 courseid
,其引用<$ c列的 id
$ c> course 表。每当我尝试在中执行
上述 saveAll()
CakePHP SQL $ c $
这是因为你的表之间有外键约束数据库。这意味着在column_id列中,您不能从外部表中插入与ids不同的值。
上面的错误表示,当您发布数据时,外部字段为空
查找内容:
1.检查您的数据库中的子表中的外部字段是否可以接受NULL。如果你有下拉的值和默认选项为空如果字段不接受NULL这个错误可能发生
2.检查你的数据在控制器中,如果你传递变量在例子:$ this-> data [ 'order'] ['course_id']如果为空或缺少,请参阅第1点。
I have two tables in my database:
order
;course
.
order
has a column courseid
which references column id
of the course
table. Whenever I tried to do saveAll()
in CakePHP
the above SQL
error will display and data wont be saved.
What it sounds is that between your tables you have foreign key constraint in the database. This mean that in the the column course_id you cannot insert values different than ids from the foreign table.
The error above means that when you post your data the foreign field is empty or missing.
What to look for: 1. Check if in your database the foreign field in the child table can accept NULL. If you have dropdown with values and the default option is empty if the field doesn't accept NULL this error could occur 2. Check your data in the controller if you pass the variable in example: $this->data['Order']['course_id'] if it's empty or missing see point 1.
这篇关于SQL错误:1452:无法添加或更新子行:外键约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!