Skip to content
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

fix: fix OmitZero and value overriding #517

Merged
merged 1 commit into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/dbtest/query_test.go
Expand Up @@ -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})?'`)
Expand Down
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-130
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-130
@@ -0,0 +1 @@
UPDATE "models" SET "updated_at" = NOW() WHERE ("id" = 0)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-130
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-130
@@ -0,0 +1 @@
UPDATE `models` AS `model` SET `updated_at` = NOW() WHERE (`model`.`id` = 0)
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-130
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING *
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-130
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING *
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-130
@@ -0,0 +1 @@
UPDATE "models" AS "model" SET "updated_at" = NOW() WHERE ("model"."id" = 0) RETURNING *
7 changes: 4 additions & 3 deletions query_update.go
Expand Up @@ -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
}

Expand All @@ -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
Expand Down