Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
larrymjordan committed May 25, 2020
1 parent 9a9d5be commit 4367abf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions preload_associations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions preload_associations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 4367abf

Please sign in to comment.