Good afternoon (at least around here),
I have a many-to-many relationship schema that I'm having trouble building. The main problem is that I'm only working with primary and foreign keys (no varchars or enums to simplify things) and the number of many-to-many relationships is not predictable and can increase at any time.
I looked around at various questions and couldn't find something that directly addressed this issue.
I split the problem in half, so I now have two one-to-many schemas. One is solved but the other is giving me fits.
Let's assume table FOO is a standard, boring table that has a simple primary key. It's the one in the one-to-many relationship.
Table BAR can relate to multiple keys of FOO. The number of related keys is not known beforehand.
An example:
From a query FOO returns ids 3, 4, 5.
BAR needs a unique key that relates to 3, 4, 5 (though there could be any number of ids returned)
The usual join table does not work:
Table FOO_BAR
primary_key | foo_id | bar_id |
Since FOO returns 3 unique keys and here bar_id has a one-to-one relationship with foo_id.
Having two join tables does not seem to work either, as it still can't map foo_ids 3, 4, 5 to a single bar_id.
Table FOO_TO_BAR
primary_key | foo_id | bar_to_foo_id |
Table BAR_TO_FOO
primary_key | foo_to_bar_id | bar_id |
What am I doing wrong? Am I making things more complicated than they are? How should I approach the problem? Thanks a lot for the help.