theshank

theshank

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?

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

I’m pretty sure Repo.update_all does that? I think?

theshank

theshank OP

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?

OvermindDL1

OvermindDL1

Good question, if it does not have a way to accept a list of updates, it should. ^.^;

theshank

theshank OP

Thanks.. will go with the raw sql query then.

gregvaughn

gregvaughn

hehe, yeah, I just did this in raw sql (via Ecto.Adapters.SQL.query!) about an hour ago :smiley:

theshank

theshank OP

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.

LostKobrakai

LostKobrakai

Ecto.Type.dump/2 might do that.

theshank

theshank OP

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?

voger

voger

Why? SQL has it’s own functions for datetime if you want to go that route.
I am a noob too when it comes to SQL.

Here are PostgreSQL date functions if you use PostgreSQL. PostgreSQL: Documentation: 9.6: Date/Time Functions and Operators

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.

theshank

theshank OP

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.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews