From 2fb2c0d3b20dd20a2fc8017c4f0b302ee6069a88 Mon Sep 17 00:00:00 2001 From: Wendell Sun Date: Thu, 14 Mar 2019 02:33:42 +0800 Subject: [PATCH] return empty slice for many2many if no asscociation was found --- callback_query_preload.go | 16 +++++++++++----- preload_test.go | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/callback_query_preload.go b/callback_query_preload.go index d7c8a133..a936180a 100644 --- a/callback_query_preload.go +++ b/callback_query_preload.go @@ -391,14 +391,20 @@ func (scope *Scope) handleManyToManyPreload(field *Field, conditions []interface key := toString(getValueFromFields(indirectScopeValue, foreignFieldNames)) fieldsSourceMap[key] = append(fieldsSourceMap[key], indirectScopeValue.FieldByName(field.Name)) } - for source, link := range linkHash { - for i, field := range fieldsSourceMap[source] { + + for source, fields := range fieldsSourceMap { + for _, f := range fields { //If not 0 this means Value is a pointer and we already added preloaded models to it - if fieldsSourceMap[source][i].Len() != 0 { + if f.Len() != 0 { continue } - field.Set(reflect.Append(fieldsSourceMap[source][i], link...)) - } + v := reflect.MakeSlice(f.Type(), 0, 0) + if len(linkHash[source]) > 0 { + v = reflect.Append(f, linkHash[source]...) + } + + f.Set(v) + } } } diff --git a/preload_test.go b/preload_test.go index 1db625c9..1a6a5d49 100644 --- a/preload_test.go +++ b/preload_test.go @@ -771,6 +771,7 @@ func TestNestedPreload11(t *testing.T) { levelB3 := &LevelB3{ Value: "bar", LevelB1ID: sql.NullInt64{Valid: true, Int64: int64(levelB1.ID)}, + LevelB2s: []*LevelB2{}, } if err := DB.Create(levelB3).Error; err != nil { t.Error(err)