diff --git a/callbacks/create.go b/callbacks/create.go index 7bc45a6c..634f402b 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -278,6 +278,11 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { field.Set(rv, curTime) values.Values[i][idx], _ = field.ValueOf(rv) } + } 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) + } } } diff --git a/finisher_api.go b/finisher_api.go index 73424dc2..7dfb72c6 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -70,7 +70,7 @@ func (db *DB) Save(value interface{}) (tx *DB) { if _, ok := tx.Statement.Clauses["ON CONFLICT"]; !ok { tx = tx.Clauses(clause.OnConflict{UpdateAll: true}) } - tx.callbacks.Create().Execute(tx) + tx.callbacks.Create().Execute(tx.InstanceSet("gorm:update_track_time", true)) case reflect.Struct: if err := tx.Statement.Parse(value); err == nil && tx.Statement.Schema != nil { for _, pf := range tx.Statement.Schema.PrimaryFields { diff --git a/tests/update_test.go b/tests/update_test.go index df709cff..be3e6fc9 100644 --- a/tests/update_test.go +++ b/tests/update_test.go @@ -606,6 +606,18 @@ func TestSave(t *testing.T) { t.Fatalf("failed to find updated user") } + user2 := *GetUser("save2", Config{}) + DB.Create(&user2) + + time.Sleep(time.Second) + user1UpdatedAt := result.UpdatedAt + var users = []*User{&result, &user2} + DB.Save(&users) + + if user1UpdatedAt == result.UpdatedAt { + t.Fatalf("user's updated at should be changed, expects: %+v, got: %+v", user1UpdatedAt, result.UpdatedAt) + } + dryDB := DB.Session(&gorm.Session{DryRun: true}) stmt := dryDB.Save(&user).Statement if !regexp.MustCompile("WHERE .id. = [^ ]+$").MatchString(stmt.SQL.String()) {