Replies: 2 comments 1 reply
-
According to your description this can be implemented without any breaking change, which means it's not something I will work on at least till 2.0 is released (That release already took too long, so I don't want to block it on any more features).
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Has this been implemented? Currently dealing w/ the same problem and am not seeing a solution in the docs, though I'm a bit of a SQL noob so I could be missing something. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This continues on from #1517
Basically, it is currently impossible(?) to do arbitrary updates to multiple distinct rows with a single query. As an imaginary example to demonstrate what I mean, let's suppose we're selling tickets to some venue that allocates seats to the customers at the end of the ticket period rather than up-front. So:
Let's naively model it with a single table for illustration purposes
When customer buys a ticket, they get put on in
customers
with their personal data butseat_id
stays NULL.After time passes, we now have some software allocate a seat_id to every customer.
Currently with diesel, we can use
AsChangeset
to update a single customer, maybe it'd look something likewe should be able to now update a single customer like so:
So far so good, but what happens if our venue has 50_000 seats? The code below doesn't compile:
So we're force to instead loop over
allocations
and runupdate
for each element. This means 50_000 small queries.At the very least postgres is able to do this in one (albeit large) query: see this StackOverflow for an example. I believe that crux is the
FROM (VALUES …)
where you can put all the values you want at once and then execute them withUPDATE
andSET
as usual.To answer the questions from the original ticket:
Ability to update all the rows specified in one query if the underlying DB supports it.
I'm not too in-depth to all the APIs but my naive suggestion would be to have a trait like
impl<T: AsChangeset> AsChangeset for &[T]
or something along these lines. Maybe we have to make use of.values
, unsure.I only know of postgres so I can't comment on others. Quick search shows sqlite should be able to do it as well as mysql.
Where it's not easily supported, diesel could just fall back to doing a loop with one query per update. That would give consistent API at least.
I don't know the exact versions but I think this has been a thing in mysql and postgres for many years now, definitely not a new feature.
See above for some links.
I don't think there's anything that's not already done with the vanilla
AsChangeset
Beta Was this translation helpful? Give feedback.
All reactions