preload null judgment

This commit is contained in:
ningfei 2021-11-03 23:03:15 +08:00
parent c170af11e9
commit 371f6371fe
2 changed files with 11 additions and 1 deletions

View File

@ -186,6 +186,9 @@ func BuildQuerySQL(db *gorm.DB) {
}
func Preload(db *gorm.DB) {
if db.Statement.Schema == nil {
return
}
if db.Error == nil && len(db.Statement.Preloads) > 0 {
preloadMap := map[string]map[string][]interface{}{}
for name := range db.Statement.Preloads {

View File

@ -133,5 +133,12 @@ func TestCount(t *testing.T) {
if err := DB.Model(&User{}).Select("*").Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).Count(&count10).Error; err != nil || count10 != 3 {
t.Fatalf("Count should be 3, but got count: %v err %v", count10, err)
}
var count11 int64
if err := DB.Table("users").
Where("name in ?", []string{user1.Name, user2.Name, user3.Name}).
Preload("Toys", func(db *gorm.DB) *gorm.DB {
return db.Table("toys").Select("name")
}).Count(&count11).Limit(2).Find(&users).Error; err != nil || count11 != 3 || len(users) !=2 {
t.Fatalf("Count should be 3,len should be 2, but got count: %v len %v err %v", count11,len(users), err)
}
}