update returning preload
This commit is contained in:
parent
c00cf29ccc
commit
6ede807f8d
@ -68,6 +68,7 @@ func RegisterDefaultCallbacks(db *gorm.DB, config *Config) {
|
|||||||
updateCallback.Register("gorm:before_update", BeforeUpdate)
|
updateCallback.Register("gorm:before_update", BeforeUpdate)
|
||||||
updateCallback.Register("gorm:save_before_associations", SaveBeforeAssociations(false))
|
updateCallback.Register("gorm:save_before_associations", SaveBeforeAssociations(false))
|
||||||
updateCallback.Register("gorm:update", Update(config))
|
updateCallback.Register("gorm:update", Update(config))
|
||||||
|
updateCallback.Register("gorm:preload", Preload)
|
||||||
updateCallback.Register("gorm:save_after_associations", SaveAfterAssociations(false))
|
updateCallback.Register("gorm:save_after_associations", SaveAfterAssociations(false))
|
||||||
updateCallback.Register("gorm:after_update", AfterUpdate)
|
updateCallback.Register("gorm:after_update", AfterUpdate)
|
||||||
updateCallback.Match(enableTransaction).Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
|
updateCallback.Match(enableTransaction).Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
|
||||||
|
@ -134,8 +134,10 @@ func Preload(db *gorm.DB) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err := preloadDB.Statement.Parse(db.Statement.Dest); err != nil {
|
if err := preloadDB.Statement.Parse(db.Statement.Dest); err != nil {
|
||||||
|
if err := preloadDB.Statement.Parse(db.Statement.Model); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
preloadDB.Statement.ReflectValue = db.Statement.ReflectValue
|
preloadDB.Statement.ReflectValue = db.Statement.ReflectValue
|
||||||
preloadDB.Statement.Unscoped = db.Statement.Unscoped
|
preloadDB.Statement.Unscoped = db.Statement.Unscoped
|
||||||
|
|
||||||
|
@ -775,6 +775,7 @@ func TestUpdateReturning(t *testing.T) {
|
|||||||
GetUser("update-returning-1", Config{}),
|
GetUser("update-returning-1", Config{}),
|
||||||
GetUser("update-returning-2", Config{}),
|
GetUser("update-returning-2", Config{}),
|
||||||
GetUser("update-returning-3", Config{}),
|
GetUser("update-returning-3", Config{}),
|
||||||
|
GetUser("update-returning-4", Config{Pets: 1}),
|
||||||
}
|
}
|
||||||
DB.Create(&users)
|
DB.Create(&users)
|
||||||
|
|
||||||
@ -795,6 +796,18 @@ func TestUpdateReturning(t *testing.T) {
|
|||||||
if results[1].Age-results[0].Age != 100 {
|
if results[1].Age-results[0].Age != 100 {
|
||||||
t.Errorf("failed to return updated age column")
|
t.Errorf("failed to return updated age column")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var result User
|
||||||
|
DB.Model(&result).Where("name = ?", users[3].Name).Clauses(clause.Returning{}).Preload("Pets").Update("age", 38)
|
||||||
|
if result.Age != 38 {
|
||||||
|
t.Errorf("failed to return updated data, got %v", results)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Pets) != 1 {
|
||||||
|
t.Errorf("failed to preload pets, got %v", result.Pets)
|
||||||
|
}
|
||||||
|
|
||||||
|
CheckPet(t, *result.Pets[0], *users[3].Pets[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateWithDiffSchema(t *testing.T) {
|
func TestUpdateWithDiffSchema(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user