<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="126789" data-post-id="126789">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jdumont" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jdumont/120/2353_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jdumont
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="peerreynders" data-post="20" data-topic="21881">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>Focusing on this statement in particular I’m not convinced you are dealing with a polymorphic entity in your domain.</p>
</blockquote>
</aside>
<p>You’re correct. As I’ve read more about this, I’ve learnt that polymorphism isn’t the right term for what I’m after. It applies to the inverse of what I’m after, but both requirements bring up a lot of the same discussion about inheritance (single table, concrete, etc) and also polymorphism.</p>
<aside class="quote no-group" data-username="peerreynders" data-post="20" data-topic="21881">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>so it may be enough to have a <code>Content</code> table which identifies (document and) type (inferring a distinct table) and a key into that table.</p>
</blockquote>
</aside>
<p>That’s the conclusion I’ve been circling around, regardless of whether I use foreign linked tables or inheritance. I’d use a column per type for the ID (exclusive arc) rather than a single column for ID that uses the type column to determine which table to lookup (would compromise foreign keys).</p>
<aside class="quote no-group" data-username="peerreynders" data-post="20" data-topic="21881">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>How much information do you need to grab on this query?</p>
</blockquote>
</aside>
<p>In this case, the index that would be on the Content table and all the fields in whatever schema it links to. In the case of the CrossFit app, there would be lots of joins happening from each schema (off to many different other tables, depending on schema).</p>
<aside class="quote no-group" data-username="OvermindDL1" data-post="21" data-topic="21881">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>You’d just have a schema for each link, mapping to the tables directly most easily.</p>
</blockquote>
</aside>
<p>That’s what I’m after.</p>
<aside class="quote no-group" data-username="OvermindDL1" data-post="21" data-topic="21881">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/48/2677_2.png" class="avatar"> OvermindDL1:</div>
<blockquote>
<p>Honestly I rarely if ever do that, I use schemas to just get what I want and use a query to build it up linking it all within the query itself, but I’m a lot more comfortable in SQL than most. ^.^;</p>
</blockquote>
</aside>
<p>I think this is the issue here, I’ve got enough SQL knowledge to do the basics, but anything more complex is all greek to me.</p>
<p>—</p>
<p>I think in light of this conversation, I’ll go away and map out exactly what data I need to be storing and importantly how I’ll be querying / joining on it. That should give me a better idea of how best to approach the problem.</p>
<p>At this stage I <em>think</em> I understand the various solutions, I just have to pick the most appropriate one.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126789" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/sql-structure-for-many-different-child-entities/21881/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-126789" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126789"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="126798" data-post-id="126798">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="peerreynders" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/120/5826_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  peerreynders
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="jdumont" data-post="22" data-topic="21881">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jdumont/48/2353_2.png" class="avatar"> jdumont:</div>
<blockquote>
<p>I’d use a column per type for the ID (exclusive arc) rather than a single column for ID that uses the type column to determine which table to lookup (would compromise foreign keys).</p>
</blockquote>
</aside>
<p>That’s the recommended approach for Ecto<br>
<a href="https://media.pragprog.com/titles/wmecto/code/priv/repo/migrations/20180620125250_add_notes_tables.exs" rel="noopener nofollow ugc">https://media.pragprog.com/titles/wmecto/code/priv/repo/migrations/20180620125250_add_notes_tables.exs</a><br>
<a href="https://media.pragprog.com/titles/wmecto/code/lib/music_db/note.ex" rel="noopener nofollow ugc">https://media.pragprog.com/titles/wmecto/code/lib/music_db/note.ex</a></p>
<p>So something like</p>
<pre><code class="lang-plaintext">[Document] &lt;-&gt;  [Content]  &lt;-&gt; [Video] | [Image] | [Markup]
   id      &lt;-- document_id
                
                video_id   --&gt;   id
                image_id   --&gt;             id
               markup_id   --&gt;                       id
</code></pre>
<p>In the above <code>Video</code>, <code>Image</code>, and <code>Markup</code> can be referenced by multiple documents. If they strictly belong to one document maybe this would be more appropriate.</p>
<pre><code class="lang-plaintext">[Document] &lt;-&gt;  [Content]  &lt;-&gt;  [Video] |  [Image] | [Markup]
   id      &lt;-- document_id
                
                   id      &lt;-- content_id
                   id      &lt;--           content_id
                   id      &lt;--                     content_id
</code></pre>
<hr>
<aside class="quote no-group" data-username="jdumont" data-post="22" data-topic="21881">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jdumont/48/2353_2.png" class="avatar"> jdumont:</div>
<blockquote>
<p>all the fields in whatever schema it links to.</p>
</blockquote>
</aside>
<p>Given</p>
<aside class="quote no-group" data-username="jdumont" data-post="1" data-topic="21881">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jdumont/48/2353_2.png" class="avatar"> jdumont:</div>
<blockquote>
<p>where each has a different structure and fields,</p>
</blockquote>
</aside>
<p>I don’t think it makes sense to try to normalize <code>Video</code>, <code>Image</code>, <code>Markup</code> in to some normalized, uniform structure - so sticking with an association with <code>Content</code> probably makes sense.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126798" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/sql-structure-for-many-different-child-entities/21881/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-126798" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126798"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="126810" data-post-id="126810">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="jdumont" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/jdumont/120/2353_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  jdumont
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think that unless I find any issues after looking into the required queries further; this is the solution I’ll go with. I’ll also revisit my recursive structure in the CrossFit app and see whether this would simplify it — I suspect it would.</p>
<p>Thanks for all your help <a class="mention" href="/u/peerreynders" rel="nofollow">@peerreynders</a> &amp; <a class="mention" href="/u/overminddl1" rel="nofollow">@OvermindDL1</a></p>
<p>I’ll be sure to post up a gist of this solution in full once I’ve built it so that it can help others.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126810" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/sql-structure-for-many-different-child-entities/21881/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-126810" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126810"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="332065" data-post-id="332065">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ostap" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ostap/120/39902_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ostap
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It seems that the PostgreSQL wiki recommends not using single table inheritance.</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_table_inheritance">
  <header class="source">
      <img src="https://wiki.postgresql.org/favicon.ico" class="site-icon" alt="" width="48" height="48">

      <a href="https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_table_inheritance" target="_blank" rel="noopener nofollow ugc">wiki.postgresql.org</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_table_inheritance" target="_blank" rel="noopener nofollow ugc">Don't use table inheritance - Don't Do This - PostgreSQL wiki</a></h3>

  <p>Don't use table inheritance. If you think you want to, use foreign keys instead.</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="332065" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/sql-structure-for-many-different-child-entities/21881/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-332065" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="332065"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #24"></div>
  </section>
</div>
    <div class="postbit" id="332068" data-post-id="332068">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sbuttgereit" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sbuttgereit/120/30162_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sbuttgereit
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>While PostgreSQL itself is an excellent database, and the PostgreSQL documentation is usually very good, too… in my experience the PostgreSQL Wiki just none of those things.  In my experience it tends to be outdated, sometimes on the order of a decade or more, and the advice is too frequently subpar as compared to other resources… even resources like Stack Overflow.  When I search for info and the Wiki comes up, I typically just ignore those results.</p>
<p>This isn’t to say that the article you’ve linked is wrong or bad;  I haven’t read the article you linked and I agree that table inheritance is usually a bad idea unless you know exactly how it works and why the nuances of your use case make it the right answer (in practice a very rare thing, but some ideas are still out there: <a href="https://www.percona.com/blog/performing-etl-using-inheritance-in-postgresql/" rel="noopener nofollow ugc">https://www.percona.com/blog/performing-etl-using-inheritance-in-postgresql/</a>).</p>
<p>I comment only to suggest that citing the Wiki as an genuinely authoritative resource is not something I’d recommend.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="332068" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/sql-structure-for-many-different-child-entities/21881/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-332068" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="332068"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>