forked from mirror/gorm
Setting gorm:auto_preload to false now prevents preloading (#2031)
This commit is contained in:
parent
0e04d414d5
commit
31ec9255cd
|
@ -14,8 +14,14 @@ func preloadCallback(scope *Scope) {
|
||||||
return
|
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)
|
autoPreload(scope)
|
||||||
|
} else if apb {
|
||||||
|
autoPreload(scope)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if scope.Search.preload == nil || scope.HasError() {
|
if scope.Search.preload == nil || scope.HasError() {
|
||||||
|
|
|
@ -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) {
|
func TestNestedPreload1(t *testing.T) {
|
||||||
type (
|
type (
|
||||||
Level1 struct {
|
Level1 struct {
|
||||||
|
|
Loading…
Reference in New Issue