Fix Change UpdatedAt to current time when doing OnConflict UpdateAll

This commit is contained in:
Jinzhu 2021-01-11 19:16:47 +08:00
parent b864a5457a
commit de850edb4f
2 changed files with 19 additions and 4 deletions

View File

@ -281,7 +281,7 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) {
} else if field.AutoUpdateTime > 0 {
if _, ok := stmt.DB.InstanceGet("gorm:update_track_time"); ok {
field.Set(rv, curTime)
values.Values[0][idx], _ = field.ValueOf(rv)
values.Values[i][idx], _ = field.ValueOf(rv)
}
}
}

View File

@ -156,8 +156,8 @@ func TestUpdates(t *testing.T) {
t.Errorf("User2's name should be updated")
}
if user2.UpdatedAt.Format(time.RFC1123) == user3.UpdatedAt.Format(time.RFC1123) {
t.Errorf("User's updated at should be changed, old %v, new %v", user2.UpdatedAt.Format(time.RFC1123), user3.UpdatedAt.Format(time.RFC1123))
if user2.UpdatedAt.Format(time.RFC1123Z) == user3.UpdatedAt.Format(time.RFC1123Z) {
t.Errorf("User's updated at should be changed, old %v, new %v", user2.UpdatedAt.Format(time.RFC1123Z), user3.UpdatedAt.Format(time.RFC1123Z))
}
// update with gorm exprs
@ -615,13 +615,28 @@ func TestSave(t *testing.T) {
time.Sleep(time.Second)
user1UpdatedAt := result.UpdatedAt
user2UpdatedAt := user2.UpdatedAt
var users = []*User{&result, &user2}
DB.Save(&users)
if user1UpdatedAt == result.UpdatedAt {
if user1UpdatedAt.Format(time.RFC1123Z) == result.UpdatedAt.Format(time.RFC1123Z) {
t.Fatalf("user's updated at should be changed, expects: %+v, got: %+v", user1UpdatedAt, result.UpdatedAt)
}
if user2UpdatedAt.Format(time.RFC1123Z) == user2.UpdatedAt.Format(time.RFC1123Z) {
t.Fatalf("user's updated at should be changed, expects: %+v, got: %+v", user2UpdatedAt, user2.UpdatedAt)
}
DB.First(&result)
if user1UpdatedAt.Format(time.RFC1123Z) == result.UpdatedAt.Format(time.RFC1123Z) {
t.Fatalf("user's updated at should be changed after reload, expects: %+v, got: %+v", user1UpdatedAt, result.UpdatedAt)
}
DB.First(&user2)
if user2UpdatedAt.Format(time.RFC1123Z) == user2.UpdatedAt.Format(time.RFC1123Z) {
t.Fatalf("user2's updated at should be changed after reload, expects: %+v, got: %+v", user2UpdatedAt, user2.UpdatedAt)
}
dryDB := DB.Session(&gorm.Session{DryRun: true})
stmt := dryDB.Save(&user).Statement
if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(stmt.SQL.String()) {