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.
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:
Can you tell me how to convert (cast?) an elixir DateTime (say the value I get from of DateTime.utc_now()), into a Postgres column of type timestamp without time zone.
Thats great! I am new to the whole Elixir ecosystem and Ecto. Thank you for pointing me in the right direction.
Just to confirm, i can use the value obtained from Ecto.Type.dump(:utc_datetime, DateTime.utc_now()) in my raw sql query. Does my understanding appear correct?
Also I am not sure what exactly does your query do in your first post. As I said I am a noob also. But I had a situation recently where I had to update multiple records with different values. I ended reading those records from the database (Repo.all(query...)) then iterating over them and appending in a Multi and finally applying everything in a transaction. A total of 2 database transactions. No need for raw SQL.
I don’t know if it is proper or if there are better ways. Just throwing an idea.
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.