forked from mirror/gorm
Merge pull request #1199 from jugglinmike/empty-associations
Return empty slice for associations with 0 results
This commit is contained in:
commit
ab703afe97
|
@ -186,9 +186,11 @@ func (scope *Scope) handleHasManyPreload(field *Field, conditions []interface{})
|
|||
for j := 0; j < indirectScopeValue.Len(); j++ {
|
||||
object := indirect(indirectScopeValue.Index(j))
|
||||
objectRealValue := getValueFromFields(object, relation.AssociationForeignFieldNames)
|
||||
if results, ok := preloadMap[toString(objectRealValue)]; ok {
|
||||
f := object.FieldByName(field.Name)
|
||||
if results, ok := preloadMap[toString(objectRealValue)]; ok {
|
||||
f.Set(reflect.Append(f, results...))
|
||||
} else {
|
||||
f.Set(reflect.MakeSlice(f.Type(), 0, 0))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -90,6 +90,8 @@ func TestPreload(t *testing.T) {
|
|||
}
|
||||
} else if len(user.Emails) != 0 {
|
||||
t.Errorf("should not preload any emails for other users when with condition")
|
||||
} else if user.Emails == nil {
|
||||
t.Errorf("should return an empty slice to indicate zero results")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -592,8 +594,14 @@ func TestNestedPreload9(t *testing.T) {
|
|||
},
|
||||
Level2_1: Level2_1{
|
||||
Level1s: []Level1{
|
||||
{Value: "value3-3"},
|
||||
{Value: "value4-4"},
|
||||
{
|
||||
Value: "value3-3",
|
||||
Level0s: []Level0{},
|
||||
},
|
||||
{
|
||||
Value: "value4-4",
|
||||
Level0s: []Level0{},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -657,6 +665,7 @@ func TestNestedPreload10(t *testing.T) {
|
|||
},
|
||||
{
|
||||
Value: "bar 2",
|
||||
LevelA3s: []*LevelA3{},
|
||||
},
|
||||
}
|
||||
for _, levelA2 := range want {
|
||||
|
|
Loading…
Reference in New Issue