binarypaladin
Okay, short story:
Let’s say I want to a user-defined SQL function or even a subquery to set a value on an insert or an update with Ecto.Repo combined with a changeset.
So, as a simple example, let’s just say I have some timestamp field that I want to set to NOW() in PostgreSQL rather than rely on the application server setting the time. I know I can do this with a fragment but… this is often done in conjunction with a pretty standard changeset cycle. If the changeset is valid, it’s pretty common to add some automatic values not set by the client.
I’m hoping there is some option I can add on Repo.insert or Repo.update that, combined with returning will result in what I am looking for without me having to do a bunch of destructuring and recombining.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #ai
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
Sounds like you want what the
read_after_writesoption toEcto.Schema.fielddoes.binarypaladin
This still doesn’t explain how to set the value to a fragment combined with a changeset.
read_after_writesis pretty much the same as always listing the field in thereturningoption.Let’s say I have a field called
voidedand if it’s set there I want to also setvoided_attoNOW(). Forget all the ways I could do this and just presume I want the application to say, “hey, during this update, setvoided_attoNOW().”read_after_writesorreturningwill get the value back to me, but how do I set it?sodapopcan
Ecto.Changeset.prepare_changes/2 possibly?
dimitarvp
Before trying to give you a working solution I’ll ask you this:
Why do you need
voidedat all? It’s basicallyvoided_at IS NOT NULL. It seems superfluous.binarypaladin
Dang. I thought that was going to be it. Nope.
After looking through a bunch of docs last night, I’m probably going to have to write my own Repo function to handle this.
binarypaladin
You’re focusing too much on a contrived example. Here’s another one (also contrived). I want to be able to do something like this:
If I wanted the date generated by the server, not the application, is there a way to do this either in a changeset or as an option in
update(and similarly, withinsert)?dimitarvp
Ah, so you need something similar to how
updated_atbehaves but not pertaining to the entire record, more like for individual fields.LostKobrakai
:autogeneratethe functionality used for timestamps does compute the datetimes in elixir as well, not in the db.dimitarvp
Yep I’m aware, simply trying to understand the problem that OP is trying to solve.
kwando
I do not know how to to this as you wish on the Elixir side.. but I would probably go for a trigger and :read_after_write if I absolutely had to do it this way