Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Strengths of the first</h2> <p>The first schema obeys better normalization rules, and so is probably better in most cases.</p> <p>Having a <code>thread_id</code>, which is basically a natural key, that isn't a FK to another table is probably asking for trouble. It will be very difficult to enforce that it is unique when you want it to be, and the same when you want it to be. For this reason, I would encourage the first suggested schema.</p> <h2>Strengths of the second</h2> <p>Your second schema allows the subject to be altered for each message in the thread. If this is a feature you want, you can't use the first option, as you've written it (but see below).</p> <h2>Other options</h2> <pre><code>Message - id - parent (fk to Message.id) - subject - content - timestamp - sender (fk) MessageRecipient - message_id (fk) - recipient (fk) - status (read, unread, deleted) </code></pre> <p>Instead of having a <code>thread_id</code> concept, you can intead have a <code>parent</code> concept. Then every reply will point to the original message's record. This allows threading, without a 'thread' table. Another possible advantage of this, is it allows <em>thread trees</em> as well. Simply put, you can represent much more complicated relationships between messages and replies this way. If you don't care about that, then this won't be a bonus for your application.</p> <p>If you don't care about the threading advantages I just mentioned, I would probably recommend a hybrid of your two schemas:</p> <pre><code>MessageThread(models.Model): - id Message(models.Model): - thread (pk) - subject - content - timestamp - sender MessageRecipient - message_id (pk) - recipient (pk) - status (read, unread, deleted) </code></pre> <p>This is similar to first schema, except that I moved the 'subject' column from the <code>MessageThread</code> to the <code>Message</code> table, to allow the subject to change as the thread progresses... I'm simply using the MessageThread table to act as a constraint on the thread ID used in Message (which overcomes the limitations I mentioned at the beginning of my answer). You may have additional meta data you want to include in the MessageThread table as well, but I'll leave that up to you and your application.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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