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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="demem123" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  demem123
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hi!</p>
<p>I’m still stuck. <img src="https://forum.elixirforum.com/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"> Thanks for the help! I’m still very new to this. <img src="https://forum.elixirforum.com/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p>I have to work with:</p>
<p>2 tables like this below:</p>
<blockquote>
<p>create table(:posts_tags, primary_key: false) do<br>
add :post_id, references(:posts)<br>
add :tag_id, references(:tags)<br>
end</p>
</blockquote>
<blockquote>
<p>create table(:posts) do<br>
add :title<br>
add :body<br>
timestamps()<br>
end</p>
</blockquote>
<blockquote>
<p>create table(:tags) do<br>
add :name<br>
timestamps()<br>
end</p>
</blockquote>
<ol start="4">
<li>
<p>in my post model in phoenix, i have 2 many_to_many tags since I have 2 tables i want to preload with<br>
many_to_many :tags_a, MyApp.Tag, join_through: “posts_tags”<br>
many_to_many :tags_b, MyApp.Tag, join_through: “posts_tags”</p>
</li>
<li>
<p>I want to do a single join to preload the struct but its not working <img src="https://forum.elixirforum.com/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p>from q in query,<br>
left_join: pa in assoc(q, :tags_a),<br>
left_join: pb in assoc(q, :tags_b),<br>
preload:  [prints_a: pa, prints_b: pb]</p>
<p>test_a = from t in query,<br>
join: pa in assoc(t, :tags_a),<br>
where: t.id == pa.tag_id</p>
<p>test_b = from t in query,<br>
join: pb in assoc(t, :tags_b),<br>
where: t.id == pb.tag_id</p>
<p>from q in query,<br>
preload: [pa: ^test_a, pb: ^test_b]</p>
</li>
</ol> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32449" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-32449" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32449"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="32758" data-post-id="32758">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelclark" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelclark/120/38296_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelclark
                      <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="demem123" data-post="12" data-topic="1274">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/d/b38774/48.png" class="avatar"> demem123:</div>
<blockquote>
<p>many_to_many :tags_a, MyApp.Tag, join_through: “posts_tags”<br>
many_to_many :tags_b, MyApp.Tag, join_through: “posts_tags”</p>
</blockquote>
</aside>
<p>If you are trying to associate many tags (i.e. tag_a and tag_b) with many (or a single) post(s) then you don’t need two <code>many to many</code> lines in your post Schema like you have.  You could delete one and rename it to just <code>many_to_many :tags</code>.</p>
<p>Alternatively, you could delete your join table and add a <code>tag_a_id</code> column and <code>tab_b_id</code> column to your <code>posts</code> table, then use two <code>has many</code> lines and <code>belongs_to</code> lines in your schemas.  However unless there is some reason you only want two tags, your join table is probably a better solution.</p>
<p>Can you give an example of what you expect your query to return given certain rows in your database?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32758" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-32758" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32758"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="32763" data-post-id="32763">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelclark" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelclark/120/38296_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelclark
                      <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>Based on your tables, I would have this schema:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule MyQuery.Post do
  use MyQuery.Web, :model

  schema "posts" do
    field :title, :string
    field :body, :string
    many_to_many :tags, MyQuery.Tag, join_through: "posts_tags"

    timestamps()
  end
end

defmodule MyQuery.PostTag do
  use MyQuery.Web, :model

  schema "posts_tags" do
    belongs_to :post, MyQuery.Post
    belongs_to :tag, MyQuery.Tag

    timestamps()
  end
end

defmodule MyQuery.Tag do
  use MyQuery.Web, :model

  schema "tags" do
    field :name, :string
    many_to_many :posts, MyQuery.Post, join_through: "posts_tags"

    timestamps()
  end
end
</code></pre>
<p>With 2 posts, 3 tags, and post1 associated with tags 1 &amp; 2 and post 2 associated with tags 2 &amp; 3:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(15)&gt; Repo.all(Post)
[debug] QUERY OK source="posts" db=1.2ms
SELECT p0."id", p0."title", p0."body", p0."inserted_at", p0."updated_at" FROM "posts" AS p0 []
[%MyQuery.Post{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts"&gt;,
  body: "Body Text", id: 1, inserted_at: ~N[2017-05-12 02:45:40.606380],
  tags: #Ecto.Association.NotLoaded&lt;association :tags is not loaded&gt;,
  title: "Title1", updated_at: ~N[2017-05-12 02:45:40.606387]},
 %MyQuery.Post{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts"&gt;,
  body: "Body Text", id: 2, inserted_at: ~N[2017-05-12 02:46:01.298796],
  tags: #Ecto.Association.NotLoaded&lt;association :tags is not loaded&gt;,
  title: "Title2", updated_at: ~N[2017-05-12 02:46:01.298802]}]

iex(16)&gt; Repo.all(Tag)
[debug] QUERY OK source="tags" db=0.8ms
SELECT t0."id", t0."name", t0."inserted_at", t0."updated_at" FROM "tags" AS t0 []
[%MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 1,
  inserted_at: ~N[2017-05-12 02:44:03.558857], name: "Sports",
  posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
  updated_at: ~N[2017-05-12 02:44:03.567081]},
 %MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 2,
  inserted_at: ~N[2017-05-12 02:44:03.588083], name: "News",
  posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
  updated_at: ~N[2017-05-12 02:44:03.588089]},
 %MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 3,
  inserted_at: ~N[2017-05-12 02:44:03.590854], name: "Opinion",
  posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
  updated_at: ~N[2017-05-12 02:44:03.590859]}]

iex(17)&gt; Repo.all(PostTag)
[debug] QUERY OK source="posts_tags" db=0.9ms
SELECT p0."id", p0."post_id", p0."tag_id", p0."inserted_at", p0."updated_at" FROM "posts_tags" AS p0 []
[%MyQuery.PostTag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts_tags"&gt;, id: 1,
  inserted_at: ~N[2017-05-12 02:47:30.915457],
  post: #Ecto.Association.NotLoaded&lt;association :post is not loaded&gt;,
  post_id: 1, tag: #Ecto.Association.NotLoaded&lt;association :tag is not loaded&gt;,
  tag_id: 1, updated_at: ~N[2017-05-12 02:47:30.915462]},
 %MyQuery.PostTag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts_tags"&gt;, id: 2,
  inserted_at: ~N[2017-05-12 02:48:01.578237],
  post: #Ecto.Association.NotLoaded&lt;association :post is not loaded&gt;,
  post_id: 1, tag: #Ecto.Association.NotLoaded&lt;association :tag is not loaded&gt;,
  tag_id: 2, updated_at: ~N[2017-05-12 02:48:01.578242]},
 %MyQuery.PostTag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts_tags"&gt;, id: 3,
  inserted_at: ~N[2017-05-12 02:48:20.769033],
  post: #Ecto.Association.NotLoaded&lt;association :post is not loaded&gt;,
  post_id: 2, tag: #Ecto.Association.NotLoaded&lt;association :tag is not loaded&gt;,
  tag_id: 2, updated_at: ~N[2017-05-12 02:48:20.769039]},
 %MyQuery.PostTag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts_tags"&gt;, id: 4,
  inserted_at: ~N[2017-05-12 02:48:39.862811],
  post: #Ecto.Association.NotLoaded&lt;association :post is not loaded&gt;,
  post_id: 2, tag: #Ecto.Association.NotLoaded&lt;association :tag is not loaded&gt;,
  tag_id: 3, updated_at: ~N[2017-05-12 02:48:39.862816]}]
</code></pre>
<p>Here is how I would preload the tags on post1 (you can see the two tags loaded under the tags key):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(19)&gt; query = from p in Post, where: p.id == 1, preload: [:tags] 
#Ecto.Query&lt;from p in MyQuery.Post, where: p.id == 1, preload: [:tags]&gt;

iex(20)&gt; post1 = Repo.all(query)                                    
[debug] QUERY OK source="posts" db=1.3ms
SELECT p0."id", p0."title", p0."body", p0."inserted_at", p0."updated_at" FROM "posts" AS p0 WHERE (p0."id" = 1) []
[debug] QUERY OK source="tags" db=2.7ms
SELECT t0."id", t0."name", t0."inserted_at", t0."updated_at", p1."id" FROM "tags" AS t0 INNER JOIN "posts" AS p1 ON p1."id" = ANY($1) INNER JOIN "posts_tags" AS p2 ON p2."post_id" = p1."id" WHERE (p2."tag_id" = t0."id") ORDER BY p1."id" [[1]]
[%MyQuery.Post{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts"&gt;,
  body: "Body Text", id: 1, inserted_at: ~N[2017-05-12 02:45:40.606380],
  tags: [%MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 1,
    inserted_at: ~N[2017-05-12 02:44:03.558857], name: "Sports",
    posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
    updated_at: ~N[2017-05-12 02:44:03.567081]},
   %MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 2,
    inserted_at: ~N[2017-05-12 02:44:03.588083], name: "News",
    posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
    updated_at: ~N[2017-05-12 02:44:03.588089]}], title: "Title1",
  updated_at: ~N[2017-05-12 02:45:40.606387]}]
</code></pre>
<p>It looks like you are trying to join and then preload, but your post row is going to be duplicated in the join to match the two tag rows and the tags will be preloaded on the duplicate posts.  This may be why you are getting an unexpected result:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(21)&gt; query2 = from p in Post, join: t in assoc(p, :tags), where: p.id == 1, preload: [:tags]
#Ecto.Query&lt;from p in MyQuery.Post, join: t in assoc(p, :tags), where: p.id == 1, preload: [:tags]&gt;

iex(22)&gt; post2 = Repo.all(query2)                                               
[debug] QUERY OK source="posts" db=3.5ms
SELECT p0."id", p0."title", p0."body", p0."inserted_at", p0."updated_at" FROM "posts" AS p0 INNER JOIN "posts_tags" AS p2 ON p2."post_id" = p0."id" INNER JOIN "tags" AS t1 ON p2."tag_id" = t1."id" WHERE (p0."id" = 1) []
[debug] QUERY OK source="tags" db=2.6ms
SELECT t0."id", t0."name", t0."inserted_at", t0."updated_at", p1."id" FROM "tags" AS t0 INNER JOIN "posts" AS p1 ON p1."id" = ANY($1) INNER JOIN "posts_tags" AS p2 ON p2."post_id" = p1."id" WHERE (p2."tag_id" = t0."id") ORDER BY p1."id" [[1]]
[%MyQuery.Post{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts"&gt;,
  body: "Body Text", id: 1, inserted_at: ~N[2017-05-12 02:45:40.606380],
  tags: [%MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 1,
    inserted_at: ~N[2017-05-12 02:44:03.558857], name: "Sports",
    posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
    updated_at: ~N[2017-05-12 02:44:03.567081]},
   %MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 2,
    inserted_at: ~N[2017-05-12 02:44:03.588083], name: "News",
    posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
    updated_at: ~N[2017-05-12 02:44:03.588089]}], title: "Title1",
  updated_at: ~N[2017-05-12 02:45:40.606387]},
 %MyQuery.Post{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "posts"&gt;,
  body: "Body Text", id: 1, inserted_at: ~N[2017-05-12 02:45:40.606380],
  tags: [%MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 1,
    inserted_at: ~N[2017-05-12 02:44:03.558857], name: "Sports",
    posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
    updated_at: ~N[2017-05-12 02:44:03.567081]},
   %MyQuery.Tag{__meta__: #Ecto.Schema.Metadata&lt;:loaded, "tags"&gt;, id: 2,
    inserted_at: ~N[2017-05-12 02:44:03.588083], name: "News",
    posts: #Ecto.Association.NotLoaded&lt;association :posts is not loaded&gt;,
    updated_at: ~N[2017-05-12 02:44:03.588089]}], title: "Title1",
  updated_at: ~N[2017-05-12 02:45:40.606387]}]
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32763" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-32763" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32763"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="32775" data-post-id="32775">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="demem123" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  demem123
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hey! thanks so much for the response! I really appreciate it! I have sleepless nights! <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>
<p>I cant modify the existing structure of the database. But i have 2 post_tags table because both tables might contain different information.</p>
<p>Example:<br>
posts table has id = 1<br>
one of the posts_tags table can have post_id = 1, tag_id = 100<br>
the other posts_tags table can have post_id = 1, tag_id = 200 (or even none at all) so I used a left join.</p>
<p>In my model, I want to be able to load:<br>
%Post{<br>
tags_a: [{ tag_id: 100 info…}, ]<br>
tags_b: <span class="chcklst-box fa fa-square-o"></span><br>
}</p>
<p>preload by itself without the join works but i was wondering if I can convert it to a single query (trying to optimize unsuccessfully) <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>
<p>cause in the ecto docs i have seen<br>
Repo.all from p in Post,<br>
join: c in assoc(p, :comments),<br>
where: c.published_at &gt; p.updated_at,<br>
preload: [comments: c]</p>
<p>so I thought I can do<br>
Repo.all from p in Post, join:t in assoc(p, :tags_a), join: t2 in assoc(p, :tags_b), preload: [tags_a: t, tags_b: t2]</p>
<p>=(</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32775" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-32775" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32775"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="32778" data-post-id="32778">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelclark" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelclark/120/38296_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelclark
                      <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>Do you have a :tags_a and a :tags_b key in your Post schema?  With a join table, you’ll get both tags in a list under tags, not two keys, tags_a and tags_b.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32778" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-32778" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32778"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="32779" data-post-id="32779">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelclark" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelclark/120/38296_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelclark
                      <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>Also, preload does the left join for you under the hood.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32779" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-32779" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32779"
                     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 #16"></div>
  </section>
</div>
    <div class="postbit" id="32781" data-post-id="32781">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelclark" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelclark/120/38296_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelclark
                      <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>One last thing, there isn’t a concept of tag_a and tag_b in your posts table, just tags through your posts_tags join table.</p>
<p>Sorry for short answers, on phone, commuting.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32781" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-32781" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32781"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="32782" data-post-id="32782">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelclark" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelclark/120/38296_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelclark
                      <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="demem123" data-post="15" data-topic="1274">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/d/b38774/48.png" class="avatar"> demem123:</div>
<blockquote>
<p>Repo.all from p in Post,<br>
join: c in assoc(p, :comments),<br>
where: c.published_at &gt; p.updated_at,<br>
preload: [comments: c]</p>
</blockquote>
</aside>
<p>The join in this query is necessary to compare the <code>published_at</code> dates in the <code>comments</code> to the <code>updated_at</code> dates in the <code>posts</code>.  The join is not necessary to preload the comments (preload does the left join on its own all with the same query).</p>
<p>The comments are preloaded for all posts that meet the</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">join: c in assoc(p, :comments),
where: c.published_at &gt; p.updated_at
</code></pre>
<p>criteria.</p>
<aside class="quote no-group" data-username="demem123" data-post="15" data-topic="1274">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/d/b38774/48.png" class="avatar"> demem123:</div>
<blockquote>
<p>%Post{<br>
tags_a: [{ tag_id: 100 info…}, ]<br>
tags_b: <span class="chcklst-box fa fa-square-o"></span><br>
}</p>
</blockquote>
</aside>
<p>What you should be getting back in your queries are:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># with one tag
%Post{
   tags: [{ tag_id: 100 info...}]
   }

# with no tags
%Post{
   tags: []
   }

# with two tags
%Post{
   tags: [{ tag_id: 100 info...}, { tag_id: 200 info...}]
   }
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir"></code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32782" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-32782" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32782"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="32804" data-post-id="32804">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="demem123" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  demem123
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>thanks! <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="32804" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-32804" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="32804"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="263931" data-post-id="263931">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Blessing" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Blessing
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m still getting the same error even tho I used different names for the schema fields.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">schema "users" do
    has_many :accounts, Account
    has_one :selected_account, Account, foreign_key: :selected_user_account_id
end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">schema "accounts" do
    belongs_to :user, User
    belongs_to :user, User, foreign_key: :selected_user_account_id
end
</code></pre>
<p>The error is:</p>
<blockquote>
<p>field/association :user already exists on schema, you must either remove the duplication or choose a different name</p>
</blockquote>
<p><strong>Updated</strong><br>
I still have the error after adding an explicit <code>foreign_key</code>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">schema "users" do
    has_many :accounts, Account, foreign_key: :all_user_accounts_id
    has_one :selected_account, Account, foreign_key: :selected_user_account_id
end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">schema "accounts" do
    belongs_to :user, User, foreign_key: :all_user_accounts_id
    belongs_to :user, User, foreign_key: :selected_user_account_id
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="263931" 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/add-two-foreign-key-columns-that-reference-the-same-table/1274/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-263931" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="263931"
                     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 #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/1274/load_more?page=3">Load more posts</a>
</div></template></turbo-stream>