sqlboiler icon indicating copy to clipboard operation
sqlboiler copied to clipboard

Update single object with Where clause?

Open kizjig opened this issue 5 years ago • 3 comments

I'm using SqlBoiler in my project and it's been going well - so thanks for making it 👍

I'm wonder if SqlBoiler supports updating a single table model object with a where clause. Specifically we'd like to have some logic to avoid stale updates of a single object in the style of...

psudeo sql code:- update … where <update_column> = same_value_we_have

update … where updated_at = myobj.updated_at

So I'd imagine the SqlBoiler logic wrapper syntax might look something like...

rowsAff, err := myModel.Update(db, boil.Infer()).Where("updated_at = ?", myModel.UpdatedAt)

Is there any support for something like this that still makes use of the object wrappers and is better than just writing raw sql update statements?

kizjig avatar May 27 '20 23:05 kizjig

I don't think we have any way to do this. Since you can't do a conditional update from a .Update() on an already-selected model.

aarondl avatar Jun 17 '20 23:06 aarondl

Another ask on a similar topic is to be able to control the set part of update clause. So we could do things like

  • update ... set counter = counter + 1 ...
  • update ... set props = props || '{"json": "rules"}'

Set value based on computation involving an existing value (column or anything else in db)

edoshor avatar Sep 26 '20 12:09 edoshor

I think this is a basic feature... How can we gracefully translate "UPDATE ... SET ... = ... WHERE ..." into sqlboiler?

caozhuozi avatar Nov 13 '22 16:11 caozhuozi