Two foreign keys on the same column from one table

北城余情 提交于 2019-12-12 09:44:07

问题


i have a project table which has a image_id field and a newsimage_id field.

Both are linked to the image table. But InnoDB doesn't allow me to set a foreign key for both fields to the same column (id).

Is there a way I can do this or is it not possible? I'm using MySQL through MAMP.

Thanks in advance!!


回答1:


Here's how I did it (MySQL 5.0.45):

ALTER TABLE `job_dependency`
ADD FOREIGN KEY (`job`) REFERENCES `job` (`id`),
ADD FOREIGN KEY (`dependency`) REFERENCES `job` (`id`);

There are problems with ON DELETE CASCADE in this situation, so don't use it.



来源:https://stackoverflow.com/questions/2638001/two-foreign-keys-on-the-same-column-from-one-table

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