diff --git a/internal/dbtest/query_test.go b/internal/dbtest/query_test.go index 73bd2209e..b5a6c79fb 100644 --- a/internal/dbtest/query_test.go +++ b/internal/dbtest/query_test.go @@ -786,6 +786,18 @@ func TestQuery(t *testing.T) { WherePK(). WhereAllWithDeleted() }, + func(db *bun.DB) schema.QueryAppender { + type Model struct { + ID int64 `bun:",pk"` + UpdatedAt time.Time + } + return db.NewUpdate(). + Model(&Model{}). + OmitZero(). + WherePK(). + Value("updated_at", "NOW()"). + Returning("*") + }, } timeRE := regexp.MustCompile(`'2\d{3}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?(\+\d{2}:\d{2})?'`) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mariadb-130 b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-130 new file mode 100644 index 000000000..0b39a3045 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mariadb-130 @@ -0,0 +1 @@ +UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-130 b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-130 new file mode 100644 index 000000000..32f7d0914 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mssql2019-130 @@ -0,0 +1 @@ +UPDATE "models" SET "updated_at" = NOW() WHERE ("id" = 0) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql5-130 b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-130 new file mode 100644 index 000000000..0b39a3045 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql5-130 @@ -0,0 +1 @@ +UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-mysql8-130 b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-130 new file mode 100644 index 000000000..0b39a3045 --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-mysql8-130 @@ -0,0 +1 @@ +UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0) diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pg-130 b/internal/dbtest/testdata/snapshots/TestQuery-pg-130 new file mode 100644 index 000000000..0e32dd8ac --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pg-130 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING * diff --git a/internal/dbtest/testdata/snapshots/TestQuery-pgx-130 b/internal/dbtest/testdata/snapshots/TestQuery-pgx-130 new file mode 100644 index 000000000..0e32dd8ac --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-pgx-130 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING * diff --git a/internal/dbtest/testdata/snapshots/TestQuery-sqlite-130 b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-130 new file mode 100644 index 000000000..0e32dd8ac --- /dev/null +++ b/internal/dbtest/testdata/snapshots/TestQuery-sqlite-130 @@ -0,0 +1 @@ +UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING * diff --git a/query_update.go b/query_update.go index e77c93554..f6b9499fa 100644 --- a/query_update.go +++ b/query_update.go @@ -273,7 +273,9 @@ func (q *UpdateQuery) appendSetStruct( isTemplate := fmter.IsNop() pos := len(b) for _, f := range fields { - if q.omitZero && f.HasZeroValue(model.strct) { + app, hasValue := q.modelValues[f.Name] + + if !hasValue && q.omitZero && f.HasZeroValue(model.strct) { continue } @@ -290,8 +292,7 @@ func (q *UpdateQuery) appendSetStruct( continue } - app, ok := q.modelValues[f.Name] - if ok { + if hasValue { b, err = app.AppendQuery(fmter, b) if err != nil { return nil, err