Skip to content

Commit 4367abf

Browse files
committed
fix test
1 parent 9a9d5be commit 4367abf

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

preload_associations.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,12 +418,13 @@ func preloadBelongsTo(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMetaI
418418
}
419419

420420
// 3) iterate over every model and fill it with the assoc.
421+
idField := mmi.getDBFieldTaggedWith(asoc.targetPrimaryID())
421422
mmi.iterate(func(mvalue reflect.Value) {
422423
modelAssociationField := mmi.mapper.FieldByName(mvalue, asoc.Name)
423424
for i := 0; i < slice.Elem().Len(); i++ {
424425
asocValue := slice.Elem().Index(i)
425-
if mmi.mapper.FieldByName(mvalue, fi.Path).Interface() == mmi.mapper.FieldByName(asocValue, "ID").Interface() ||
426-
reflect.DeepEqual(mmi.mapper.FieldByName(mvalue, fi.Path), mmi.mapper.FieldByName(asocValue, "ID")) {
426+
if mmi.mapper.FieldByName(mvalue, fi.Path).Interface() == mmi.mapper.FieldByName(asocValue, idField.Path).Interface() ||
427+
reflect.DeepEqual(mmi.mapper.FieldByName(mvalue, fi.Path), mmi.mapper.FieldByName(asocValue, idField.Path)) {
427428

428429
switch {
429430
case modelAssociationField.Kind() == reflect.Slice || modelAssociationField.Kind() == reflect.Array:

preload_associations_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func Test_New_Implementation_For_Nplus1_BelongsTo_Primary_ID(t *testing.T) {
224224
userAttrs := []UserAttribute{}
225225
a.NoError(tx.EagerPreload("User").All(&userAttrs))
226226
a.Len(userAttrs, 2)
227-
a.Equal("Mark", userAttrs[0].UserName)
228-
a.Equal("Mark", userAttrs[1].UserName)
227+
a.Equal("Mark", userAttrs[0].User.UserName)
228+
a.Equal("Mark", userAttrs[1].User.UserName)
229229
})
230230
}

0 commit comments

Comments
 (0)