This error can occur due to some reasons as mentioned below
1. Data Types may not be the same for fk and pk.
2. Size may not be the same of fk and pk
3. type may be different like unsigned, binary,unsigned binary and null
4. The value you are trying to put in city does not occur in area table
yii null foreign key
-
15-10-2022 - |
Question
I have such structure of db http://pikucha.ru/icFsc (I can't add pictures here)
Some addresses my not have a metro (other tables have the same problem, for example "user" may not have an address, but there is constraint in "user" table)
If I add a record in mysql it's ok. If I do the same thing using yii I get an error
Cannot add or update a child row: a foreign key constraint fails (
address
, CONSTRAINT fk_Address_Area1
FOREIGN KEY (area_id
) REFERENCES area
(id
) ON UPDATE NO ACTION)
So, what's the problem in?
Solution
OTHER TIPS
This is because you relation doesn't allow NULL
values in the column. You have to edit the specific column, and check the empty
checkbox. Then MySQL will allow NULL
values.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow