Change UpdatedAt to current time when doing OnConflict UpdateAll
This commit is contained in:
		
							parent
							
								
									a5bfe2f39d
								
							
						
					
					
						commit
						d888c799d7
					
				| @ -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) | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 
 | ||||
|  | ||||
| @ -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 { | ||||
|  | ||||
| @ -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()) { | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu