theshank
Is there any equivalent of `update ... from` syntax for Postgresql in Ecto?
My use case is perfectly served by the update ... from syntax for Postgres as per the query below:
update test as t set
column_a = c.column_a,
column_c = c.column_c
from (values
('123', 1, '---'),
('345', 2, '+++')
) as c(column_b, column_a, column_c)
where c.column_b = t.column_b;
This is taken from this question on Stackoverflow.
Is there any way to achieve this using ecto?
Most Liked
theshank
I believe the “values” that will be there in the updated records when using update_all will be the same for all the updated records.
In my use case, I have a list of ids and the new value for a particular field for each of those ids. The next values are different for each id. Something like below:
[ [1, New_value_for_some_col_for_rec_1],
[2, New_value_for_some_col_for_rec_2],
...
]
update_all wont work for this case… right?
theshank
Thanks.. will go with the raw sql query then.
theshank
Thanks for both the suggestions!
Using Ecto.Multi might also be a good option… My use-case is to update a few thousand records every minute with new values. These records are just recurring “jobs” that have a “scheduled_time”. Every minute, the jobs that have completed in the past minute are updated with the new values for the “scheduled_time” for the next run.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









