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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Not sure why this hasn’t been mentioned before (maybe this is a new addition in Ecto), but this seems to be  the answer to this issue <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#foreign_key_constraint/3" class="inline-onebox" rel="noopener nofollow ugc">Ecto.Changeset — Ecto v3.14.0</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="150707" 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/ecto-validating-belongs-to-association-is-not-nil/2665/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-150707" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="150707"
                     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="152901" data-post-id="152901">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I had the same problem recently and this thread was very helpful.</p>
<aside class="quote no-group quote-modified" data-username="DragosMocrii" data-post="22" data-topic="2665" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dragosmocrii/48/13125_2.png" class="avatar"> DragosMocrii:</div>
<blockquote>
<p>Not sure why this hasn’t been mentioned before (maybe this is a new addition in Ecto), but this seems to be the answer to this issue <a href="https://hexdocs.pm/ecto/Ecto.Changeset.html#foreign_key_constraint/3" class="inline-onebox" rel="noopener nofollow ugc">Ecto.Changeset — Ecto v3.14.0</a></p>
</blockquote>
</aside>
<p>It seems that <code>foreign_key_constraint</code> will only work if you provide the foreign key field while <code>cast_assoc</code> will work if you provide a map - hence <a class="mention" href="/u/wojtekmach" rel="nofollow">@wojtekmach</a>’s solution to validate both cases.</p>
<p>For future reference, a simple use-case to illustrate how it can be done individually might be…</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">schema "posts" do
  field :title, :string
  has_many :comments, Comment
end

schema "comments" do
  field :text, :string
  belongs_to :post, Post
end
</code></pre>
<p><strong>Inserting a comment referencing an existing post:</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def changeset(comment, attrs) do
  comment
  |&gt; cast(attrs, [:text])
  |&gt; assoc_constraint(:post) # or foreign_key_constraint(:post_id)
  |&gt; validate_required([:text])
end
</code></pre>
<p><code>create_comment(%{text: "Nice post!", post_id: 1 })</code></p>
<p><strong>Inserting a comment with a post all at once:</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def changeset(comment, attrs) do
  comment
  |&gt; cast(attrs, [:text])
  |&gt; cast_assoc(:post, required: true)
  |&gt; validate_required([:text])
end
</code></pre>
<p><code>create_comment(%{text: "Nice post!", post: %{title: "How to..." } })</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="152901" 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/ecto-validating-belongs-to-association-is-not-nil/2665/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-152901" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="152901"
                     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="153447" data-post-id="153447">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is apples and oranges. <code>foreign_key_constraint</code> just provides a changeset error message when a constraint in the database is violated - which happens if you provide a foreign key value that is not defined in the referenced table. It provides nothing in terms of casting associations, or ensuring that associations are defined.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="153447" 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/ecto-validating-belongs-to-association-is-not-nil/2665/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-153447" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="153447"
                     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="159069" data-post-id="159069">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p><strong>Inserting a comment with a post all at once:</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def changeset(comment, attrs) do
  comment
  |&gt; cast(attrs, [:text])
  |&gt; cast_assoc(:post, required: true)
  |&gt; validate_required([:text])
end
</code></pre>
</blockquote>
<p>If I may ask (being quite new to phoenix), using cast_assoc makes the test suuuper slow (about 500ms, compared to some milliseconds without inserting this extra record). We’re inserting two records here instead of one; but still; am i right to doubt such duration is normal ?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="159069" 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/ecto-validating-belongs-to-association-is-not-nil/2665/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-159069" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="159069"
                     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="296857" data-post-id="296857">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve searched a lot and it still doesn’t seem like there’s a good way to handle the case where you’re creating a child record for a has_many/belongs_to by just passing in the foreign key as an :assoc_id param?</p>
<p>I can’t seem to find anything that actually validates if assoc_id is nil, apart from validates_required…but the docs explicitly say don’t do that. <code>foreign_key_constraint</code> doesn’t do anything if the key is nil/null. <code>cast_assoc</code> expects that you’re modifying/creating the related record, rather just referencing it.</p>
<p>Is the only recommended way to go via the parent?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="296857" 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/ecto-validating-belongs-to-association-is-not-nil/2665/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-296857" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="296857"
                     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>