From 4367abf56e2ed5b594b9232887544fb3da01e8c8 Mon Sep 17 00:00:00 2001 From: Larry M Jordan Date: Mon, 25 May 2020 13:38:40 -0500 Subject: [PATCH] fix test --- preload_associations.go | 5 +++-- preload_associations_test.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/preload_associations.go b/preload_associations.go index 53c80fea..e94dc179 100644 --- a/preload_associations.go +++ b/preload_associations.go @@ -418,12 +418,13 @@ func preloadBelongsTo(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMetaI } // 3) iterate over every model and fill it with the assoc. + idField := mmi.getDBFieldTaggedWith(asoc.targetPrimaryID()) mmi.iterate(func(mvalue reflect.Value) { modelAssociationField := mmi.mapper.FieldByName(mvalue, asoc.Name) for i := 0; i < slice.Elem().Len(); i++ { asocValue := slice.Elem().Index(i) - if mmi.mapper.FieldByName(mvalue, fi.Path).Interface() == mmi.mapper.FieldByName(asocValue, "ID").Interface() || - reflect.DeepEqual(mmi.mapper.FieldByName(mvalue, fi.Path), mmi.mapper.FieldByName(asocValue, "ID")) { + if mmi.mapper.FieldByName(mvalue, fi.Path).Interface() == mmi.mapper.FieldByName(asocValue, idField.Path).Interface() || + reflect.DeepEqual(mmi.mapper.FieldByName(mvalue, fi.Path), mmi.mapper.FieldByName(asocValue, idField.Path)) { switch { case modelAssociationField.Kind() == reflect.Slice || modelAssociationField.Kind() == reflect.Array: diff --git a/preload_associations_test.go b/preload_associations_test.go index a300842b..e26ad9bd 100644 --- a/preload_associations_test.go +++ b/preload_associations_test.go @@ -224,7 +224,7 @@ func Test_New_Implementation_For_Nplus1_BelongsTo_Primary_ID(t *testing.T) { userAttrs := []UserAttribute{} a.NoError(tx.EagerPreload("User").All(&userAttrs)) a.Len(userAttrs, 2) - a.Equal("Mark", userAttrs[0].UserName) - a.Equal("Mark", userAttrs[1].UserName) + a.Equal("Mark", userAttrs[0].User.UserName) + a.Equal("Mark", userAttrs[1].User.UserName) }) }