OvermindDL1

OvermindDL1

Ecto Update list of matching values

So what would be the Ecto way to do this:

  • Need to update potentially a lot of rows.
  • The conditionals is a list, all the same fields to match but different values for every one.
  • Also a static value to update on them all (think updated_at).

In PostgreSQL you’d generally do something like this:

UPDATE blah AS b SET
  field1 = v.field1,
  field5 = "newvalue"
FROM (VALUES
  ("something", "bloop", 42, 6.28),
  ("another", "bleep", 6, 1.1),
  ...morevalue
) as v(field1, field2, field3, field4)
WHERE
  b.field2 = v.field2 AND
  b.field3 = v.field3 AND
  b.field4 = v.field4

Or in a transaction create a temporary table, fill it, and join across them or so.

None of these I can seem to figure out how to do in Ecto-speak due to query design limitations, so I’d have to drop down to raw SQL (yay not catching typing bugs…), so that’s what I’m doing for now (via temp table).

Anyone have ideas on how would this pattern be done in normal Ecto without dropping to SQL? Given a static value to update in addition to a list of values that both include things to match on as well as values to update on a per-row basis.

I guess I could do a dynamic build chain with an utterly ginormously massive WHERE clause, but I don’t think that would be terribly efficient and would break upon a certain size…

First Post! Switch mode

acrolink

acrolink

I wish you had provided some sample data / tables to understand what you want to do. Not that I will have an answer, but out of curiosity :slight_smile: to understand the SQL statement above.

Most Liked

OvermindDL1

OvermindDL1

In essence, given this table:

field0field1field2field3updated_at
0"hello""test"421990-01-01
1"world""tester"131990-02-01
2"goodbye""testing"181990-03-01
etc...

And want to do something like WHERE field1 = "hello" AND field2 = "test" then set field0 to something like 18 and update the updated_at, except repeat this for a lot of rows.

Last Post!

scottming

scottming

I have the same problem, in the end, which way you used?

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement