Clear SET clause after build SQL

This commit is contained in:
Jinzhu 2023-05-15 15:43:44 +08:00
parent 63534145fd
commit c3d7d08b9a
2 changed files with 6 additions and 1 deletions

View File

@ -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

View File

@ -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)