Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL schema design question around relating 2 different types of ID's to one piece of information
    primarykey
    data
    text
    <p>I'm working on redesigning some parts of our schema, and I'm running into a problem where I just don't know a good clean way of doing something. I have an event table such as:</p> <pre><code>Events -------- event_id </code></pre> <p>for each event, there could be n groups or users associated with it. So there's a table relating Events to Users to reflect that one to many relationship such as:</p> <pre><code>EventUsers ---------- event_id user_id </code></pre> <p>The problem is that we also have a concept of groups. We want to potentially tie n groups to an event in addition to users. So, that user_id column isn't sufficient, because we need to store potentially either a user_id or a group_id.</p> <p>I've thought of a variety of ways to handle this, but they all seem like a big hack. For example, I could make that a participant_id and put in a participant_type column such as:</p> <pre><code>EventUsers ---------- event_id participant_id participant_type </code></pre> <p>and if I wanted to get the events that user_id 10 was a part of, it could be something like:</p> <pre><code>select event_id from EventUsers where participant_id = 10 and participant_type = 1 </code></pre> <p>(assuming that somewhere participant_type 1 was defined to be a User). But I don't like that from a philosophical point of view because when I look at the data, I don't know what the number in participant_id means unless I also look at the value in particpant_type.</p> <p>I could also change EventUsers to be something like:</p> <pre><code>EventParticipants ----------------- event_id user_id group_id </code></pre> <p>and allow the values of user_id and group_id to be NULL if that record is dealing with the other type of information.</p> <p>Of course, I could just break EventUsers and we'll call it EventGroups into 2 different tables but I'd like to keep who is tied to an event stored in one single place if there's a good logical way to do it.</p> <p>So, am I overlooking a good way to accomplish this?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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