Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there an elegant way to store a dual relationship (i.e. user 1 and user 2 are friends)
    primarykey
    data
    text
    <p>I've run into the same problem in two different pieces of work this month:</p> <pre><code>Version 1: User 1 &amp; User 2 are friends Version 2: Axis 1 &amp; Axis 2 when graphed should have the quadrants colored... </code></pre> <p>The problem is, I don't see an elegant way, using a RDBMS, to store and query this information.</p> <p>There are two obvious approaches:</p> <p>Approach 1:</p> <pre><code>store the information twice (i.e. two db rows rows per relationship): u1, u2, true u2, u1, true u..n, u..i, true u..i, u..n, true have rules to always look for the inverse on updates: on read, no management needed on create, create inverse on delete, delete inverse on update, update inverse Advantage: management logic is always the same. Disadvantage: possibility of race conditions, extra storage (which is admittedly cheap, but feels wrong) </code></pre> <p>Approach 2:</p> <pre><code>store the information once (i.e. one db row per relationship) u1, u2, true u..n, u..i, true have rules to check for corollaries: on read, if u1, u2 fails, check for u2, u1 on create u1, u2: check for u2, u1, if it doesn't exist, create u1, u2 on delete, no management needed on update, optionally redo same check as create Advantage: Only store once Disadvantage: Management requires different set of cleanup depending on the operation </code></pre> <p>I'm wondering if there's a 3rd approach that goes along the lines of "key using f(x,y) where f(x,y) is unique for every x,y combination and where f(x,y) === f(y,x)" </p> <p>My gut tells me that there should be some combination of bitwise operations that can fulfill these requirements. Something like a two-column:</p> <p>key1 = x &amp;&amp; y key2 = x + y </p> <p>I'm hoping that people who spent more time in the math department, and less time in the sociology department have seen a proof of the possibility or impossibility of this and can provide a quick "[You moron,] its easily proven (im)possible, see this link" (name calling optional)</p> <p>Any other elegant approach would also be very welcome.</p> <p>Thanks</p>
    singulars
    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.
 

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