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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="kwando" data-post="11" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kwando/48/4092_2.png" class="avatar"> kwando:</div>
<blockquote>
<p>I would probably go for a trigger</p>
</blockquote>
</aside>
<p>I was also going to suggest a trigger.  I <em>am</em> curious as to why your are so set on doing it on the db side.  None of my business, of course, lol.  If it’s not too late to convert your timestamp fields to <code>utc_datetime_usec</code> then it won’t matter if you set it in Elixir or on the db.  But again, if you have a very good reason for doing it in the db then so be it!  I’m just not sure of the cleanest way to do it.  You could always use a Multi but that is two steps.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287843" 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/combining-server-generated-values-with-changsets/55764/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-287843" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287843"
                     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="287849" data-post-id="287849">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="binarypaladin" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/binarypaladin/120/31708_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  binarypaladin
                    <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="kwando" data-post="11" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kwando/48/4092_2.png" class="avatar"> kwando:</div>
<blockquote>
<p>but I would probably go for a trigger and :read_after_write if I absolutely had to do it this way</p>
</blockquote>
</aside>
<p>Triggers are typically how I would handle this and I don’t even use <code>:read_after_write</code> but manually specify <code>:returning</code> when I know I need it.</p>
<p>With that said, on a Ruby project I worked on a while back, there were times where the execution logic for a function took data outside the scope of the SQL row from the application for a calculation. A trigger won’t work in this case because the trigger is only going to deal with data in SQL.</p>
<aside class="quote no-group" data-username="sodapopcan" data-post="12" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>If it’s not too late to convert your timestamp fields to <code>utc_datetime_usec</code> then it won’t matter if you set it in Elixir or on the db.</p>
</blockquote>
</aside>
<p>I think there is still way too much focus on the example. Timestamps are just super simple to explain because <code>NOW()</code> is a function everyone understands and the datatype that comes with it is understood. I could have a function like… <code>super_expensive_calculation_that_has_been_highly_optimized_for_sql()</code> that perhaps I want to give data from the application to the SQL function.</p>
<p>Ruby Sequel allowed me to do this really easily.</p>
<aside class="quote no-group" data-username="sodapopcan" data-post="12" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>I’m just not sure of the cleanest way to do it.</p>
</blockquote>
</aside>
<p>As soon as you have to involve the database more explicitly, some of the “cleanliness” goes away but that’s also often the price to be paid when you’re dealing with a legacy schema or something that has to be highly optimized. It’s sounding like destructure the changeset and then generate my own queries if I want to do this is very much in my future. I was just hoping it wasn’t.</p>
<aside class="quote no-group" data-username="sodapopcan" data-post="12" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>You could always use a Multi but that is two steps.</p>
</blockquote>
</aside>
<p>Multi wouldn’t work because there’s still no way that I am aware of inside multi to say, “Hey, on this step do some raw SQL.” If that were possible, I could do it on the single changeset.</p>
<p>I can wrap the operation in a transaction where I do one query via a changeset and then another “bare” query but that’s pretty inefficient. I don’t want to do this in two queries where it’s not possible.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287849" 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/combining-server-generated-values-with-changsets/55764/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-287849" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287849"
                     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="287853" data-post-id="287853">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There’s probably a much better way to do this, but a Multi would technically work if you stuck a <code>Multi.run</code> in there to grab the changed fields.  After running the <code>Multi.update</code> or <code>Multi.insert</code> you can use a <code>Multi.insert_all</code> to run a “raw” Ecto query to update the changed fields using a <code>fragment</code> or use another <code>Multi.run</code> to perform a totally raw SQL query if you wanted with <code>Repo.query</code>.  It’d be pretty dang janky, but is possible.</p>
<p>EDIT: Sorry, didn’t mean to focus on the whole “timestamp” thing, but like, you can run “raw” queries with <code>Multi</code>.  Ideally through an <code>update_all</code> but you could do <code>Repo.query</code> if you really wanted to (but shouldn’t be necessary).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287853" 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/combining-server-generated-values-with-changsets/55764/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-287853" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287853"
                     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="287854" data-post-id="287854">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hmm, sounds like it will be generating your own queries then. Depending on what else you have to do in the same query it doesn’t have to be that bad I think <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>
<p>I haven’t checked but maybe there is a way to “get an ecto query” from a changeset that you then can manipulate before sending to the 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="287854" 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/combining-server-generated-values-with-changsets/55764/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-287854" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287854"
                     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="287855" data-post-id="287855">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="kwando" data-post="15" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kwando/48/4092_2.png" class="avatar"> kwando:</div>
<blockquote>
<p>I haven’t checked but maybe there is a way to “get an ecto query” from a changeset</p>
</blockquote>
</aside>
<p>I was looking for this same thing!  Didn’t find anything, though could probably mash together an Ecto query with an <code>changeset.changes</code> fairly cleanly… maybe?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287855" 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/combining-server-generated-values-with-changsets/55764/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-287855" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287855"
                     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="287857" data-post-id="287857">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="binarypaladin" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/binarypaladin/120/31708_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  binarypaladin
                    <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>Yeah.</p>
<p>The problem with Ecto—at least, something I have observed—is there are some “impure” ways of dealing with database-specific issues. They’re cool because something like this is awesome:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Repo.insert!(
  changeset,
  conflict_target: [:col_a, :col_b],
  on_conflict: [inc: [total: num]],
  returning: [:total]
)
</code></pre>
<p>This is a great upsert situation. I think the <em>best</em> place to handle what I want would be similar—not on the changeset, but actually on the Repo functions since the Repo itself has more awareness, as it were, to the actual storage medium.</p>
<p>The problem is that I feel like they become specific rather than more generic. I dunno if I am explaining that well.</p>
<aside class="quote no-group" data-username="kwando" data-post="15" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kwando/48/4092_2.png" class="avatar"> kwando:</div>
<blockquote>
<p>I haven’t checked but maybe there is a way to “get an ecto query” from a changeset that you then can manipulate before sending to the database.</p>
</blockquote>
</aside>
<p><em>This</em> would get me like… 90% of what I want actually. With that said, looking through the actual source code for Repo, it did not look trivial and I don’t know that there’s an obvious function for this. There is a lot of stuff being handled by changesets when doing non-trivial CRUD, that’s what I gathered from that exercise.</p>
<aside class="quote no-group" data-username="sodapopcan" data-post="16" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/48/34668_2.png" class="avatar"> sodapopcan:</div>
<blockquote>
<p>I was looking for this same thing! Didn’t find anything, though could probably mash together an Ecto query with an <code>changeset.changes</code> fairly cleanly… maybe?</p>
</blockquote>
</aside>
<p>And… this is the fallback. When I say destructuring, querying, and putting it all back together… this is it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287857" 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/combining-server-generated-values-with-changsets/55764/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-287857" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287857"
                     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="287860" data-post-id="287860">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="kwando" data-post="15" data-topic="55764">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kwando/48/4092_2.png" class="avatar"> kwando:</div>
<blockquote>
<p>I haven’t checked but maybe there is a way to “get an ecto query” from a changeset that you then can manipulate before sending to the database.</p>
</blockquote>
</aside>
<p>There isn’t because a changeset doesn’t map to a single query. Association handling means there might be a multitude of queries, which might even affect each other (e.g. create parent before the child record).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="287860" 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/combining-server-generated-values-with-changsets/55764/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-287860" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287860"
                     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="287861" data-post-id="287861">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Right, I almost never use associations in changesets so I didn’t think about that <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="287861" 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/combining-server-generated-values-with-changsets/55764/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-287861" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="287861"
                     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>