From c3d7d08b9a9f861e53e8b194fcc6b7cedc4191e1 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Mon, 15 May 2023 15:43:44 +0800 Subject: [PATCH] Clear SET clause after build SQL --- callbacks/update.go | 1 + tests/update_test.go | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/callbacks/update.go b/callbacks/update.go index 4eb75788..ff075dcf 100644 --- a/callbacks/update.go +++ b/callbacks/update.go @@ -72,6 +72,7 @@ func Update(config *Config) func(db *gorm.DB) { db.Statement.AddClauseIfNotExists(clause.Update{}) if _, ok := db.Statement.Clauses["SET"]; !ok { if set := ConvertToAssignments(db.Statement); len(set) != 0 { + defer delete(db.Statement.Clauses, "SET") db.Statement.AddClause(set) } else { return diff --git a/tests/update_test.go b/tests/update_test.go index 36ffa6a0..f7c36d74 100644 --- a/tests/update_test.go +++ b/tests/update_test.go @@ -208,13 +208,17 @@ func TestUpdateColumn(t *testing.T) { CheckUser(t, user1, *users[0]) CheckUser(t, user2, *users[1]) - DB.Model(users[1]).UpdateColumn("name", "update_column_02_newnew") + DB.Model(users[1]).UpdateColumn("name", "update_column_02_newnew").UpdateColumn("age", 19) AssertEqual(t, lastUpdatedAt.UnixNano(), users[1].UpdatedAt.UnixNano()) if users[1].Name != "update_column_02_newnew" { t.Errorf("user 2's name should be updated, but got %v", users[1].Name) } + if users[1].Age != 19 { + t.Errorf("user 2's name should be updated, but got %v", users[1].Age) + } + DB.Model(users[1]).UpdateColumn("age", gorm.Expr("age + 100 - 50")) var user3 User DB.First(&user3, users[1].ID)