-
-
Notifications
You must be signed in to change notification settings - Fork 724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to write a structured update using max() #1692
Comments
side note: counting all the records with queueOrder not set to nil (and then +1) would also work here. |
ok I've come up with this as my best attempt thus far. try await appDB.db.write { db in
let max = try Episode.select(max(Column("queueOrder")), as: Int.self).fetchOne(db)
episode.queueOrder = (max ?? 0) + 1
try episode.update(db, columns: ["queueOrder"])
} |
or! let max = try Episode.select(max(Column("queueOrder")), as: Int.self)
.fetchOne(db)
try Episode
.filter(id: episode.id)
.updateAll(db, Column("queueOrder").set(to: (max ?? 0) + 1)) |
I think I’ve probably answered my own question here :) I also need to figure out the reverse: when I want to insert an episode at the top of the queue (giving it the queueOrder value of 1) how can I update all other records that have a queueOrder set at all to being +1 of what they are now… |
Hello @jubishop, Performing two requests looks fine to me 👍
try Episode.updateAll(db, Column("queueOrder").set(to: Column("queueOrder") + 1))
// Equivalent
try Episode.updateAll(db, Column("queueOrder") += 1) |
I'm trying to use all your amazing libraries rather than write raw sql, but I'm stumped on this one. here's what works in raw sql:
How would you write this without dropping to raw sql (if possible)?
thanks again for this great library!
The text was updated successfully, but these errors were encountered: