bun icon indicating copy to clipboard operation
bun copied to clipboard

`bun.UpdateQuery`'s `.Set` method causes any other model fields to be ignored

Open ardenn opened this issue 1 year ago • 2 comments

Updates to a model's fields appear to be ignored whenever .Set is used on the query even when I add .Column. I often update a table through the model struct (account.UpdatedAt = time.Now()), but also set some fields using a query like (q.Set("update_count = update_count + 1")). Only the fields set with .Set get updated. Is this by design? I remember being able to do this with go-pg.

Example:

account.UpdatedAt = time.Now()
_, err := db.NewUpdate().Model(&account).WherePK().
	Column("updated_at","update_count").Set("update_count = update_count + 1").
	OmitZero().Returning("*").Exec(ctx)

generates the query: UPDATE account SET update_count = update_count + 1 ...

instead of UPDATE account SET updated_at = '2023-07-08T08:51:27.217Z' , update_count = update_count + 1 ...

ardenn avatar Jul 09 '23 11:07 ardenn

Based on this and this it looks like .Set and updating model struct fields are mutually exclusive - we return early as soon as len(q.set) > 0. Any chance of combining the two update sources?

ardenn avatar Jul 10 '23 16:07 ardenn

#647

qindj avatar Sep 15 '23 00:09 qindj