Skip to content

Commit

Permalink
BelongsTo uses field name instead of type for ID field
Browse files Browse the repository at this point in the history
  • Loading branch information
stangah committed Mar 6, 2018
1 parent ca27fce commit fc58739
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
4 changes: 1 addition & 3 deletions associations/belongs_to_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package associations
import (
"fmt"
"reflect"

"github.com/markbates/inflect"
)

// belongsToAssociation is the implementation for the belongs_to
Expand All @@ -23,7 +21,7 @@ func init() {

func belongsToAssociationBuilder(p associationParams) (Association, error) {
fval := p.modelValue.FieldByName(p.field.Name)
ownerIDField := fmt.Sprintf("%s%s", inflect.Capitalize(fval.Type().Name()), "ID")
ownerIDField := fmt.Sprintf("%s%s", p.field.Name, "ID")

if _, found := p.modelType.FieldByName(ownerIDField); !found {
return nil, fmt.Errorf("there is no '%s' defined in model '%s'", ownerIDField, p.modelType.Name())
Expand Down
11 changes: 5 additions & 6 deletions associations/belongs_to_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@ func (f fooBelongsTo) TableName() string {
}

type barBelongsTo struct {
FooBelongsToID uuid.UUID `db:"foo_id"`
Foo fooBelongsTo `belongs_to:"foo"`
NestedBar nestedBar `has_one:"nestedBar"`
FooID uuid.UUID `db:"foo_id"`
Foo fooBelongsTo `belongs_to:"foo"`
}

type nestedBar struct {
ID uuid.UUID `db:"id"`
fooBelongsToID uuid.UUID `db:"foo_belongs_to_id"`
ID uuid.UUID `db:"id"`
FooID uuid.UUID `db:"foo_id"`
}

func Test_Belongs_To_Association(t *testing.T) {
a := require.New(t)

id, _ := uuid.NewV1()
bar := barBelongsTo{FooBelongsToID: id}
bar := barBelongsTo{FooID: id}

as, err := associations.AssociationsForStruct(&bar, "Foo.NestedBars")
a.NoError(err)
Expand Down

0 comments on commit fc58739

Please sign in to comment.