Imagine that you live on a very simplified example of the earth - and imagine that there is a table of people in your MySQL database:
create table person ( person_id int, name text ) select * from person; +
and these people should collaborate / work together, so you have a link table that links one personโs record to another:
create table person__person ( person__person_id int, person_id int, other_person_id int )
This setting means that the links between people are unidirectional - that is, Alice can refer to Bob without linking Bob to Alice and, even worse, Alice can link Bob, and Bob can link to Alice at the same time, in two separate link entries. Since these links represent a working relationship, in the real world this is all two-way relationship. In this setting, everything is possible:
select * from person__person; +---------------------+-----------+--------------------+ | person__person_id | person_id | other_person_id | +---------------------+-----------+--------------------+ | 1 | 1 | 2 | | 2 | 2 | 1 | | 3 | 2 | 2 | | 4 | 3 | 1 | +---------------------+-----------+--------------------+
For example, with person__person_id = 4 above, when you view the Carol profile (person_id = 3), you should see a relationship with Alice (person_id = 1), and when you view the Alice profile, you should see a relationship with Carol, although the link goes to another side.
I understand that I can make allied and different queries, and still not represent the relationship as mutual in the user interface, but is there a better way? I got the feeling that there is a better way: the one where this problem will melt carefully, setting up the database correctly, but I do not see it. Has anyone got a better idea?
data-modeling many-to-many
Duncan lock
source share