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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>One notable gotcha with this approach in Ecto vs ActiveRecord: ActiveRecord’s <code>find_by</code> takes the “first” matching record and silently ignores anything else, while Ecto’s <code>get_by</code> includes enforcement that there is <em>exactly</em> one matching record and raises if not.</p>
<p>That means that if two racing calls to <code>MyApp.Repo.get_or_insert(MyItem, %{var1: "some value"})</code> ever manage to both do an insert, <strong>every</strong> subsequent call to <code>MyApp.Repo.get_or_insert(MyItem, %{var1: "some value"})</code> will crash.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333667" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-333667" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333667"
                     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="333674" data-post-id="333674">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Which is why his function’s body should be wrapped in <code>Repo.transaction</code>, and maybe with the lock type specified (depending on scenario).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333674" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-333674" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333674"
                     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="333683" data-post-id="333683">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Strong agree. <em>Most</em> use cases for this kind of function should come with a unique constraint so the <code>on_conflict</code> option works perfectly well (I’ve never run into any gotchas around the behavior of <code>returning</code>). If it’s really not possible to use a constraint I would think it’s probably worth it to use a transaction <em>and</em> a lock, since it doesn’t add that much more complexity.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333683" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-333683" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333683"
                     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="333685" data-post-id="333685">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Good shout. I’ve noticed this and planned to catch the error, but realised in my use cases it was essentially not possible to have two copies of the same record for the tables I’m using this, so went with the original suggestion from SO that doesn’t catch.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333685" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-333685" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333685"
                     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="333690" data-post-id="333690">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Can you elaborate on what “essentially not possible” means in your use case if it isn’t enforced via a unique constraint? In my experience if the database allows it, it will eventually happen.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333690" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-333690" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333690"
                     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="333773" data-post-id="333773">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>On default postgres even a transaction <em>and</em> an explicit lock is not enough to prevent a race condition where multiple rows get inserted. You can’t take an explicit lock on a row that doesn’t exist - for that you need a predicate lock (in practice a range lock), which you will get if you run under SERIALIZABLE isolation.</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://www.postgresql.org/docs/current/transaction-iso.html">
  <header class="source">
      <img src="https://www.postgresql.org/favicon.ico" class="site-icon" alt="" width="48" height="48">

      <a href="https://www.postgresql.org/docs/current/transaction-iso.html" target="_blank" rel="noopener nofollow ugc" title="01:09PM - 14 May 2026">PostgreSQL Documentation – 14 May 26</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:540/557;"><img src="https://www.postgresql.org/media/img/about/press/elephant.png" class="thumbnail" alt="" width="540" height="557"></div>

<h3><a href="https://www.postgresql.org/docs/current/transaction-iso.html" target="_blank" rel="noopener nofollow ugc">13.2.&nbsp;Transaction Isolation</a></h3>

  <p>13.2.&nbsp;Transaction Isolation # 13.2.1. Read Committed Isolation Level 13.2.2. Repeatable Read Isolation Level 13.2.3. Serializable Isolation Level The SQL standard …</p>


  </article>

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

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

<p>Even under snapshot isolation (I believe this is “repeatable read” in postgres) you will still have this anomaly. If you have two concurrent transactions they will both see that the row is missing in their snapshots and so they will both insert a new row.</p>
<p>So your options here are either to run under serializable isolation (which has a performance hit as I understand it) or to just use a unique constraint. Frankly I am struggling to think of a scenario where you want to <code>get_or_insert</code> <em>without</em> a unique constraint - the <code>get_or_insert</code> itself essentially <em>is</em> a unique constraint, albeit a buggy one.</p>
<p>Also, even if you run under serializable isolation I’m pretty sure you still won’t get an <em>atomic</em> <code>get_or_insert</code>, because postgres’s “serializable snapshot isolation” (SSI) is optimistic and I belive it will just abort one of the transactions. I <em>think</em> the only way to get a truly atomic operation is to do with Jose suggested above and use an insert/on_conflict/returning with a unique constraint.</p>
<p>Finally, there is actually another option - you can “materialize the conflict”. In this case that would mean locking another related row which you know exists. For example, imagine you had a table <code>users</code> and another table <code>user_profiles</code>, and you wanted to enforce that there is only one row in the latter for each user, and you <em>didn’t</em> want to use unique constraints <em>or</em> serializable for some reason. You could instead lock the <em>user’s</em> row in <code>users</code> before doing your <code>get_or_insert</code>, which would effectively serialize the operations and protect you from the race condition. This works because the <em>user row already exists</em>, so there’s always a row to lock on.</p>
<p>I’ve actually used this last trick for a product I’m working on, which involves some rather nightmarish logic for implementing nested drag/drop reordering with undo/redo on top of postgres rows. Clearly I need to write a blog post about this…</p>
<p>P.S. for those interested in this sort of thing there is actually a really cool paper about postgres’s SSI implementation:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://arxiv.org/abs/1208.4179">
  <header class="source">
      <img src="https://arxiv.org/static/browse/0.3.4/images/icons/favicon-32x32.png" class="site-icon" alt="" width="32" height="32">

      <a href="https://arxiv.org/abs/1208.4179" target="_blank" rel="noopener nofollow ugc">arXiv.org</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/402;"><img src="https://static.arxiv.org/icons/twitter/arxiv-logo-twitter-square.png" class="thumbnail" alt="" width="690" height="690"></div>

<h3><a href="https://arxiv.org/abs/1208.4179" target="_blank" rel="noopener nofollow ugc">Serializable Snapshot Isolation in PostgreSQL</a></h3>

  <p>This paper describes our experience implementing PostgreSQL's new serializable isolation level. It is based on the recently-developed Serializable Snapshot Isolation (SSI) technique. This is the first implementation of SSI in a production database...</p>


  </article>

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

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

<p>It’s a great read. Serializable snapshot isolation is a neat trick and is used in some distributed databases too (e.g. CockroachDB and FoundationDB). FoundationDB’s distributed SSI in particular is a really simple and beautiful algorithm. Cockroach’s on the other hand is kinda cursed but I digress.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333773" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-333773" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333773"
                     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="333777" data-post-id="333777">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>In my case I only insert records into the table through the <code>get_or_insert</code> for the given table, so unless race condition happens there (possible of course), there are no other places to insert a duplicate record.</p>
<p>I’m more after a simpler Rails <code>find_or_create</code> behaviour that doesn’t require a DB constraints and quietly returns the first matching record. To match that behaviour closer, I’ve updated my code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
  def get_or_insert(schema, params) do
    case find_first_by(schema, params) do
      nil -&gt; insert(schema.changeset(schema.__struct__, params))
      struct -&gt; {:ok, struct}
    end
  end

  import Ecto.Query, only: [where: 2, first: 1], warn: false
  def find_first_by(schema, params) do
    params = Map.to_list(params)
    where(schema, ^params) |&gt; first |&gt; one
  end

  def get_or_insert!(schema, params) do
    {:ok, record} = get_or_insert(schema, params)
    record
  end
</code></pre>
<p>In my use case duplicates aren’t the end of the world, but if it becomes an issue, I’ll look into adding constraints. I have bulky <code>jsonb</code> columns on one of the tables where I’m using <code>get_or_insert</code>, and I’m worried creating unique indexes (Postgres) might take too much storage. Would be nice to test to see if that’s indeed the case, but my time is limited, and this solutions does the trick for now.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333777" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-333777" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333777"
                     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="333779" data-post-id="333779">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="seva" data-post="18" data-topic="22160">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/seva/48/22920_2.png" class="avatar"> seva:</div>
<blockquote>
<p>I’m more after a simpler Rails <code>find_or_create</code> behaviour that doesn’t require a DB constraints and quietly returns the first matching record.</p>
</blockquote>
</aside>
<p>I don’t know what Rails does, but the reason Ecto lacks this feature is probably due to what I mentioned above: it’s unfortunately just not possible to do this safely without serializable isolation or a constraint.</p>
<aside class="quote no-group" data-username="seva" data-post="18" data-topic="22160">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/seva/48/22920_2.png" class="avatar"> seva:</div>
<blockquote>
<p>I have bulky <code>jsonb</code> columns on one of the tables where I’m using <code>get_or_insert</code>, and I’m worried creating unique indexes (Postgres) might take too much storage. Would be nice to test to see if that’s indeed the case, but my time is limited, and this solutions does the trick for now.</p>
</blockquote>
</aside>
<p>Obviously you gotta do what you gotta do, but the race will bite you <em>eventually</em>, and probably when it’s least convenient (or however the saying goes…)</p>
<p>To be clear, the json columns would only inflate your index if they’re actually <em>in</em> the constraint, i.e. they are the things that you are checking for uniqueness. That’s rather unusual, but if that’s really the case then you could hash the json (like just dump it into sha256) and then store the hash along with the data and put the constraint on that instead. I’ve actually done this exact thing (except on multiple columns) to diff RSS entries for the very same app I mentioned above.</p>
<p>Also, if the JSON is what you are checking for uniqueness, that would mean you’re doing your lookup against a column with large JSON blobs without an index. Once that gets slow (it will), you’re going to need an index. At which point, you’re back to square 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="333779" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-333779" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333779"
                     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="333791" data-post-id="333791">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’ve never had much cause to dig too deep into postgres’ full feature set around this sort of thing, precisely because a constraint is almost always called for and they are what I rely on 95% of the time.</p>
<p>For the other times however, I have used explicit locking I am wondering how to square what you’re describing with the point <a class="mention" href="/u/jeremyjh" rel="nofollow">@jeremyjh</a> makes above, which <a href="https://forum.elixirforum.com/t/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/7" rel="nofollow">specifically distinguishes isolation level from locking</a>. My understanding is two connections requesting an exclusive lock on a table in order to perform this kind of “find or insert” will protect against race conditions.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333791" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-333791" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333791"
                     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="333794" data-post-id="333794">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="seva" data-post="18" data-topic="22160">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/seva/48/22920_2.png" class="avatar"> seva:</div>
<blockquote>
<p>In my case I only insert records into the table through the <code>get_or_insert</code> for the given table, so unless race condition happens there (possible of course), there are no other places to insert a duplicate record.</p>
</blockquote>
</aside>
<p>Famous last words.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="333794" 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/how-to-do-get-or-insert-by-in-ecto-without-race-condition/22160/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-333794" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="333794"
                     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/22160/load_more?page=3">Load more posts (14 remaining)</a>
</div></template></turbo-stream>