yii null foreign key

血红的双手。 提交于 2019-12-09 21:50:54

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/21971599/yii-null-foreign-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!