From fc587395742a3dcd6627efd8e05988e01c7c7ce9 Mon Sep 17 00:00:00 2001 From: Patrick Stanger Date: Mon, 5 Mar 2018 16:45:23 -0800 Subject: [PATCH 1/4] BelongsTo uses field name instead of type for ID field --- associations/belongs_to_association.go | 4 +--- associations/belongs_to_association_test.go | 11 +++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/associations/belongs_to_association.go b/associations/belongs_to_association.go index ceebfe6e..265ac708 100644 --- a/associations/belongs_to_association.go +++ b/associations/belongs_to_association.go @@ -3,8 +3,6 @@ package associations import ( "fmt" "reflect" - - "github.com/markbates/inflect" ) // belongsToAssociation is the implementation for the belongs_to @@ -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()) diff --git a/associations/belongs_to_association_test.go b/associations/belongs_to_association_test.go index 8b4fa768..1dcd92f7 100644 --- a/associations/belongs_to_association_test.go +++ b/associations/belongs_to_association_test.go @@ -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) From 069096fac65caf82342f7dd7e3c755a35ea4a288 Mon Sep 17 00:00:00 2001 From: Patrick Stanger Date: Tue, 6 Mar 2018 12:31:10 -0800 Subject: [PATCH 2/4] Fixes Pop tests for new belongs_to naming --- finders_test.go | 2 +- pop_test.go | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/finders_test.go b/finders_test.go index b4db8f17..35e9159f 100644 --- a/finders_test.go +++ b/finders_test.go @@ -148,7 +148,7 @@ func Test_Find_Eager_Has_One_With_Inner_Associations_Struct(t *testing.T) { err = tx.Create(&composer) a.NoError(err) - coolSong := Song{Title: "Hook", UserID: user.ID, ComposerID: composer.ID} + coolSong := Song{Title: "Hook", UserID: user.ID, ComposedByID: composer.ID} err = tx.Create(&coolSong) a.NoError(err) diff --git a/pop_test.go b/pop_test.go index 1131707d..2fe257a0 100644 --- a/pop_test.go +++ b/pop_test.go @@ -148,13 +148,13 @@ type Enemy struct { } type Song struct { - ID uuid.UUID `db:"id"` - Title string `db:"title"` - UserID int `db:"u_id"` - CreatedAt time.Time `json:"created_at" db:"created_at"` - UpdatedAt time.Time `json:"updated_at" db:"updated_at"` - ComposerID int `json:"composer_id" db:"composer_id"` - ComposedBy Composer `belongs_to:"composer"` + ID uuid.UUID `db:"id"` + Title string `db:"title"` + UserID int `db:"u_id"` + CreatedAt time.Time `json:"created_at" db:"created_at"` + UpdatedAt time.Time `json:"updated_at" db:"updated_at"` + ComposedByID int `json:"composer_id" db:"composer_id"` + ComposedBy Composer `belongs_to:"composer"` } type Composer struct { From 043d3b2dd4ca2dea9c64eccfac9ec5af1e856c7c Mon Sep 17 00:00:00 2001 From: Patrick Stanger Date: Tue, 6 Mar 2018 13:07:42 -0800 Subject: [PATCH 3/4] Removes nested association from belongs_to_association tests --- associations/belongs_to_association_test.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/associations/belongs_to_association_test.go b/associations/belongs_to_association_test.go index 1dcd92f7..02a55284 100644 --- a/associations/belongs_to_association_test.go +++ b/associations/belongs_to_association_test.go @@ -12,8 +12,7 @@ import ( ) type fooBelongsTo struct { - ID uuid.UUID `db:"id"` - NestedBars []nestedBar `has_many:"nested_bars"` + ID uuid.UUID `db:"id"` } func (f fooBelongsTo) TableName() string { @@ -25,18 +24,13 @@ type barBelongsTo struct { Foo fooBelongsTo `belongs_to:"foo"` } -type nestedBar struct { - 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{FooID: id} - as, err := associations.AssociationsForStruct(&bar, "Foo.NestedBars") + as, err := associations.AssociationsForStruct(&bar, "Foo") a.NoError(err) a.Equal(len(as), 1) a.Equal(reflect.Struct, as[0].Kind()) From 41d4074edea9e236f155d39f2f79670954ba65d7 Mon Sep 17 00:00:00 2001 From: Patrick Stanger Date: Tue, 6 Mar 2018 13:35:11 -0800 Subject: [PATCH 4/4] Changes field name in test data migration --- migrations/20160808213310_setup_tests2.up.fizz | 2 +- pop_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/migrations/20160808213310_setup_tests2.up.fizz b/migrations/20160808213310_setup_tests2.up.fizz index 091e9796..02066022 100644 --- a/migrations/20160808213310_setup_tests2.up.fizz +++ b/migrations/20160808213310_setup_tests2.up.fizz @@ -2,7 +2,7 @@ create_table("songs", func(t) { t.Column("id", "uuid", {"primary":true}) t.Column("u_id", "int", {"null":true}) t.Column("title", "string", {}) - t.Column("composer_id", "int", {"null":true}) + t.Column("composed_by_id", "int", {"null":true}) }) create_table("composers", func(t) { diff --git a/pop_test.go b/pop_test.go index 2fe257a0..db0e8a42 100644 --- a/pop_test.go +++ b/pop_test.go @@ -153,7 +153,7 @@ type Song struct { UserID int `db:"u_id"` CreatedAt time.Time `json:"created_at" db:"created_at"` UpdatedAt time.Time `json:"updated_at" db:"updated_at"` - ComposedByID int `json:"composer_id" db:"composer_id"` + ComposedByID int `json:"composed_by_id" db:"composed_by_id"` ComposedBy Composer `belongs_to:"composer"` }