volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

Update single object with Where clause? #760

Open kizjig opened 4 years ago

kizjig commented 4 years ago

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?

aarondl commented 4 years ago

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.

edoshor commented 4 years ago

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

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

caozhuozi commented 1 year ago

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