Skip to content

Commit d7e5556

Browse files
committed
Merge branch 'development' of github.com:gobuffalo/pop into development
2 parents 3898c06 + 9024edb commit d7e5556

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

preload_associations.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ func (ami *AssociationMetaInfo) fkName() string {
159159
t = reflectx.Deref(t.Elem())
160160
}
161161
fkName := fmt.Sprintf("%s%s", flect.Underscore(flect.Singularize(t.Name())), "_id")
162-
return defaults.String(ami.Field.Tag.Get("fk_id"), fkName)
162+
fkNameTag := flect.Underscore(ami.Field.Tag.Get("fk_id"))
163+
return defaults.String(fkNameTag, fkName)
163164
}
164165

165166
// preload is the query mode used to load associations from database
@@ -339,6 +340,10 @@ func preloadHasOne(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMetaInfo
339340
func preloadBelongsTo(tx *Connection, asoc *AssociationMetaInfo, mmi *ModelMetaInfo) error {
340341
// 1) get all associations ids.
341342
fi := mmi.getDBFieldTaggedWith(asoc.fkName())
343+
if fi == nil {
344+
fi = mmi.getDBFieldTaggedWith(fmt.Sprintf("%s%s", flect.Underscore(asoc.Path), "_id"))
345+
}
346+
342347
fkids := []interface{}{}
343348
mmi.iterate(func(val reflect.Value) {
344349
fkids = append(fkids, mmi.mapper.FieldByName(val, fi.Path).Interface())

preload_associations_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,24 @@ func Test_New_Implementation_For_Nplus1_Nested(t *testing.T) {
182182
SetEagerMode(EagerDefault)
183183
})
184184
}
185+
186+
func Test_New_Implementation_For_Nplus1_BelongsTo_Not_Underscore(t *testing.T) {
187+
if PDB == nil {
188+
t.Skip("skipping integration tests")
189+
}
190+
transaction(func(tx *Connection) {
191+
a := require.New(t)
192+
user := User{Name: nulls.NewString("Mark")}
193+
a.NoError(tx.Create(&user))
194+
195+
taxi := Taxi{UserID: nulls.NewInt(user.ID)}
196+
a.NoError(tx.Create(&taxi))
197+
198+
SetEagerMode(EagerPreload)
199+
taxis := []Taxi{}
200+
a.NoError(tx.EagerPreload().All(&taxis))
201+
a.Len(taxis, 1)
202+
a.Equal("Mark", taxis[0].Driver.Name.String)
203+
SetEagerMode(EagerDefault)
204+
})
205+
}

0 commit comments

Comments
 (0)