Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Inner has many associations when passing on multiple arguments #633

Merged
merged 6 commits into from May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions associations/associations_for_struct.go
Expand Up @@ -34,7 +34,7 @@ func ForStruct(s interface{}, fields ...string) (Associations, error) {
}
fields = trimFields(fields)
associations := Associations{}
innerAssociations := InnerAssociations{}
fieldsWithInnerAssociation := map[string]InnerAssociations{}

// validate if fields contains a non existing field in struct.
// and verify is it has inner associations.
Expand All @@ -55,9 +55,14 @@ func ForStruct(s interface{}, fields ...string) (Associations, error) {
return associations, fmt.Errorf("field %s does not exist in model %s", fields[i], t.Name())
}

if innerField != "" {
innerAssociations = append(innerAssociations, InnerAssociation{fields[i], innerField})
if _, ok := fieldsWithInnerAssociation[fields[i]]; ok {
if innerField != "" {
fieldsWithInnerAssociation[fields[i]] = append(fieldsWithInnerAssociation[fields[i]], InnerAssociation{fields[i], innerField})
}
continue
}

fieldsWithInnerAssociation[fields[i]] = InnerAssociations{}
}

for i := 0; i < t.NumField(); i++ {
Expand All @@ -79,7 +84,7 @@ func ForStruct(s interface{}, fields ...string) (Associations, error) {
modelType: t,
modelValue: v,
popTags: tags,
innerAssociations: innerAssociations,
innerAssociations: fieldsWithInnerAssociation[f.Name],
}

a, err := builder(params)
Expand Down
40 changes: 40 additions & 0 deletions finders_test.go
Expand Up @@ -908,3 +908,43 @@ func Test_FindManyToMany(t *testing.T) {
r.NoError(err)
})
}

func Test_FindMultipleInnerHasMany(t *testing.T) {
if PDB == nil {
t.Skip("skipping integration tests")
}
transaction(func(tx *Connection) {
r := require.New(t)

user := User{Name: nulls.NewString("Mark")}
err := tx.Create(&user)
r.NoError(err)

book := Book{Title: "Pop Book", Isbn: "PB1", UserID: nulls.NewInt(user.ID)}
err = tx.Create(&book)
r.NoError(err)

writer := Writer{Name: "Jhon", BookID: book.ID}
err = tx.Create(&writer)
r.NoError(err)

friend := Friend{FirstName: "Frank", LastName: "Kafka", WriterID: writer.ID}
err = tx.Create(&friend)
r.NoError(err)

address := Address{Street: "St 27", HouseNumber: 27, WriterID: writer.ID}
err = tx.Create(&address)
r.NoError(err)

u := User{}
err = tx.Eager("Books.Writers.Addresses", "Books.Writers.Friends").Find(&u, user.ID)
r.NoError(err)

r.Len(u.Books, 1)
r.Len(u.Books[0].Writers, 1)
r.Len(u.Books[0].Writers[0].Addresses, 1)
r.Equal(u.Books[0].Writers[0].Addresses[0].HouseNumber, 27)
r.Len(u.Books[0].Writers[0].Friends, 1)
r.Equal(u.Books[0].Writers[0].Friends[0].FirstName, "Frank")
})
}
4 changes: 4 additions & 0 deletions pop_test.go
Expand Up @@ -163,6 +163,8 @@ type Books []Book
type Writer struct {
ID int `db:"id"`
Name string `db:"name"`
Addresses Addresses `has_many:"addresses"`
Friends []Friend `has_many:"friends"`
BookID int `db:"book_id"`
Book Book `belongs_to:"book"`
CreatedAt time.Time `db:"created_at"`
Expand All @@ -174,6 +176,7 @@ type Writers []Writer
type Address struct {
ID int `db:"id"`
Street string `db:"street"`
WriterID int `db:"writer_id"`
HouseNumber int `db:"house_number"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
Expand Down Expand Up @@ -207,6 +210,7 @@ func (UsersAddressQuery) TableName() string {
type Friend struct {
ID int `db:"id"`
FirstName string `db:"first_name"`
WriterID int `db:"writer_id"`
LastName string `db:"last_name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
Expand Down
1 change: 1 addition & 0 deletions testdata/migrations/20181104135526_good_friends.up.fizz
Expand Up @@ -2,5 +2,6 @@ create_table("good_friends") {
t.Column("id", "int", {primary: true})
t.Column("first_name", "string", {})
t.Column("last_name", "string", {})
t.Column("writer_id", "int",{})
t.Timestamps()
}
1 change: 1 addition & 0 deletions testdata/migrations/20181104140340_addresses.up.fizz
Expand Up @@ -2,5 +2,6 @@ create_table("addresses") {
t.Column("id", "int", {primary: true})
t.Column("street", "string", {})
t.Column("house_number", "int", {})
t.Column("writer_id", "int",{})
t.Timestamps()
}