Answer the question
In order to leave comments, you need to log in
How to solve the problem with two foreign keys?
There is a table with elements of the form:
elements
-------------------------------
`element_id ` | `someData`
-------------------------------
1 | blabla
------------------------------
2 | blablaAgain
-------------------------------
relations
------------------------------------------------
`element_1_id` | `element_2_id` | `relation`
Answer the question
In order to leave comments, you need to log in
An auxiliary table is needed to implement the ManyToMany relationship. If the relationship is OneToMany, you can get away with adding a field of type parent_id to the existing table.
I understand that this is M:M. The relation field is related to the connection itself, some additional data.
Two keys and hang:
ALTER TABLE `relations`
ADD CONSTRAINT `relations_id1_fk` FOREIGN KEY ( `element_1_id`) REFERENCES `elements` (`element_id`),
ADD CONSTRAINT `relations_id2_fk` FOREIGN KEY ( `element_1_id`) REFERENCES `elements` (`element_id`);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question