Setting gorm:auto_preload to false now prevents preloading (#2031)

This commit is contained in:
Elliott 2018-08-19 01:11:27 +02:00 committed by Jinzhu
parent 0e04d414d5
commit 31ec9255cd
2 changed files with 33 additions and 2 deletions

View File

@ -14,8 +14,14 @@ func preloadCallback(scope *Scope) {
return
}
if _, ok := scope.Get("gorm:auto_preload"); ok {
if ap, ok := scope.Get("gorm:auto_preload"); ok {
// If gorm:auto_preload IS NOT a bool then auto preload.
// Else if it IS a bool, use the value
if apb, ok := ap.(bool); !ok {
autoPreload(scope)
} else if apb {
autoPreload(scope)
}
}
if scope.Search.preload == nil || scope.HasError() {

View File

@ -123,6 +123,31 @@ func TestAutoPreload(t *testing.T) {
}
}
func TestAutoPreloadFalseDoesntPreload(t *testing.T) {
user1 := getPreloadUser("auto_user1")
DB.Save(user1)
preloadDB := DB.Set("gorm:auto_preload", false).Where("role = ?", "Preload")
var user User
preloadDB.Find(&user)
if user.BillingAddress.Address1 != "" {
t.Error("AutoPreload was set to fasle, but still fetched data")
}
user2 := getPreloadUser("auto_user2")
DB.Save(user2)
var users []User
preloadDB.Find(&users)
for _, user := range users {
if user.BillingAddress.Address1 != "" {
t.Error("AutoPreload was set to fasle, but still fetched data")
}
}
}
func TestNestedPreload1(t *testing.T) {
type (
Level1 struct {