Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilding a many-to-many db schema using only an unpredictable number of foreign keys
    primarykey
    data
    text
    <p>Good afternoon (at least around here),</p> <p>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.</p> <p>I looked around at various questions and couldn't find something that directly addressed this issue.</p> <p>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.</p> <p>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.</p> <p>Table BAR can relate to multiple keys of FOO. The number of related keys is not known beforehand.</p> <p>An example:</p> <ol> <li>From a query FOO returns ids 3, 4, 5.</li> <li>BAR needs a unique key that relates to 3, 4, 5 (though there could be any number of ids returned)</li> </ol> <p>The usual join table does not work:</p> <pre><code>Table FOO_BAR primary_key | foo_id | bar_id | </code></pre> <p>Since FOO returns 3 unique keys and here bar_id has a one-to-one relationship with foo_id.</p> <p>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.</p> <pre><code>Table FOO_TO_BAR primary_key | foo_id | bar_to_foo_id | Table BAR_TO_FOO primary_key | foo_to_bar_id | bar_id | </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload