Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase tables, one table referencing multiple unrelated tables
    primarykey
    data
    text
    <p>This question has come up a few times in various forums in my searching, but none have provided a concise resolution.</p> <p>If I have the following tables:</p> <pre><code>User +- id +- username +- password Article +- id +- title +- content </code></pre> <p>and I want to join them in order to determine who created what articles, I could simply add the column <strong>user_id</strong> to <strong>Article</strong> to use as a reference. Alternatively, I'm adding an intermediate table to show who/when/what, for example:</p> <pre><code>User +- ... Article +- ... ChangeHistory +- id +- article_id +- user_id +- type [enum(insert, update, delete)] +- datetime </code></pre> <p>Now this is fine, but the system I'm working on needs to be much more dynamic, in that new modules can be easily introduced and integrated. So now if I add a <strong>Media</strong> table I need to split the <strong>ChangeHistory</strong> between <strong>Article</strong> and <strong>Media</strong> having:</p> <pre><code>User +- ... Article +- ... Media +- id +- title +- path ArticleChangeHistory +- id +- article_id +- user_id +- type [enum(insert, update, delete)] +- datetime MediaChangeHistory +- id +- media_id +- user_id +- type [enum(insert, update, delete)] +- datetime </code></pre> <p>This can get out of hand quickly with the introduction of many modules. Each module would need to be responsible for the creation and management of it's own <strong>ChangeHistory</strong> table.</p> <p><strong>TL;DR: What practices can I explore to create an intermediate table that can receive references to multiple other unrelated tables? I could add a *record_type* field, holding the name of the table to which the record belongs, but that's ugly. I would need something like a "<em>table ID</em>" to reference the table from which it's coming. That way, when/if modules are added or removed, the model doesn't fall apart.</strong></p> <p>Any ideas? Thanks so much in advance.</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.
 

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