Fix update UpdatedAt when full saving associations, close #4115
This commit is contained in:
		
							parent
							
								
									189547f615
								
							
						
					
					
						commit
						eb9a704fda
					
				| @ -361,6 +361,7 @@ func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	tx := db.Session(&gorm.Session{NewDB: true}).Clauses(onConflict).Session(&gorm.Session{ | 	tx := db.Session(&gorm.Session{NewDB: true}).Clauses(onConflict).Session(&gorm.Session{ | ||||||
|  | 		FullSaveAssociations:     db.FullSaveAssociations, | ||||||
| 		SkipHooks:                db.Statement.SkipHooks, | 		SkipHooks:                db.Statement.SkipHooks, | ||||||
| 		DisableNestedTransaction: true, | 		DisableNestedTransaction: true, | ||||||
| 	}) | 	}) | ||||||
| @ -370,6 +371,10 @@ func saveAssociations(db *gorm.DB, rel *schema.Relationship, values interface{}, | |||||||
| 		return true | 		return true | ||||||
| 	}) | 	}) | ||||||
| 
 | 
 | ||||||
|  | 	if tx.Statement.FullSaveAssociations { | ||||||
|  | 		tx = tx.InstanceSet("gorm:update_track_time", true) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	if len(selects) > 0 { | 	if len(selects) > 0 { | ||||||
| 		tx = tx.Select(selects) | 		tx = tx.Select(selects) | ||||||
| 	} else if len(selectColumns) > 0 && len(omits) == 0 { | 	} else if len(selectColumns) > 0 && len(omits) == 0 { | ||||||
|  | |||||||
| @ -320,6 +320,11 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { | |||||||
| 						field.Set(stmt.ReflectValue, curTime) | 						field.Set(stmt.ReflectValue, curTime) | ||||||
| 						values.Values[0][idx], _ = field.ValueOf(stmt.ReflectValue) | 						values.Values[0][idx], _ = field.ValueOf(stmt.ReflectValue) | ||||||
| 					} | 					} | ||||||
|  | 				} else if field.AutoUpdateTime > 0 { | ||||||
|  | 					if _, ok := stmt.DB.InstanceGet("gorm:update_track_time"); ok { | ||||||
|  | 						field.Set(stmt.ReflectValue, curTime) | ||||||
|  | 						values.Values[0][idx], _ = field.ValueOf(stmt.ReflectValue) | ||||||
|  | 					} | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ package tests_test | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"testing" | 	"testing" | ||||||
|  | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| 	. "gorm.io/gorm/utils/tests" | 	. "gorm.io/gorm/utils/tests" | ||||||
| @ -31,7 +32,10 @@ func TestUpdateHasOne(t *testing.T) { | |||||||
| 
 | 
 | ||||||
| 	var user3 User | 	var user3 User | ||||||
| 	DB.Preload("Account").Find(&user3, "id = ?", user.ID) | 	DB.Preload("Account").Find(&user3, "id = ?", user.ID) | ||||||
|  | 
 | ||||||
| 	CheckUser(t, user2, user3) | 	CheckUser(t, user2, user3) | ||||||
|  | 	var lastUpdatedAt = user2.Account.UpdatedAt | ||||||
|  | 	time.Sleep(time.Second) | ||||||
| 
 | 
 | ||||||
| 	if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&user).Error; err != nil { | 	if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&user).Error; err != nil { | ||||||
| 		t.Fatalf("errors happened when update: %v", err) | 		t.Fatalf("errors happened when update: %v", err) | ||||||
| @ -39,7 +43,13 @@ func TestUpdateHasOne(t *testing.T) { | |||||||
| 
 | 
 | ||||||
| 	var user4 User | 	var user4 User | ||||||
| 	DB.Preload("Account").Find(&user4, "id = ?", user.ID) | 	DB.Preload("Account").Find(&user4, "id = ?", user.ID) | ||||||
| 	CheckUser(t, user4, user) | 
 | ||||||
|  | 	if lastUpdatedAt.Format(time.RFC3339) == user4.Account.UpdatedAt.Format(time.RFC3339) { | ||||||
|  | 		t.Fatalf("updated at should be updated, but not, old: %v, new %v", lastUpdatedAt.Format(time.RFC3339), user3.Account.UpdatedAt.Format(time.RFC3339)) | ||||||
|  | 	} else { | ||||||
|  | 		user.Account.UpdatedAt = user4.Account.UpdatedAt | ||||||
|  | 		CheckUser(t, user4, user) | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	t.Run("Polymorphic", func(t *testing.T) { | 	t.Run("Polymorphic", func(t *testing.T) { | ||||||
| 		var pet = Pet{Name: "create"} | 		var pet = Pet{Name: "create"} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu