Skip to content

Commit

Permalink
Merge branch 'master' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislas-m committed Feb 20, 2019
2 parents 9580258 + a140925 commit 4321166
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 12 deletions.
19 changes: 7 additions & 12 deletions associations/many_to_many_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/gobuffalo/flect"
"github.com/gobuffalo/x/defaults"
"github.com/gofrs/uuid"
)

Expand Down Expand Up @@ -72,24 +73,18 @@ func (m *manyToManyAssociation) Interface() interface{} {
// Constraint returns the content for a where clause, and the args
// needed to execute it.
func (m *manyToManyAssociation) Constraint() (string, []interface{}) {
modelColumnID := fmt.Sprintf("%s%s", flect.Underscore(m.model.Type().Name()), "_id")
modelColumnID := defaults.String(m.primaryID, fmt.Sprintf("%s%s", flect.Underscore(m.model.Type().Name()), "_id"))

var columnFieldID string
i := reflect.Indirect(m.fieldValue)
t := i.Type()
if i.Kind() == reflect.Slice || i.Kind() == reflect.Array {
t := i.Type().Elem()
columnFieldID = fmt.Sprintf("%s%s", flect.Underscore(t.Name()), "_id")
} else {
columnFieldID = fmt.Sprintf("%s%s", flect.Underscore(i.Type().Name()), "_id")
}

if m.fkID != "" {
columnFieldID = m.fkID
t = t.Elem()
}

if m.primaryID != "" {
modelColumnID = m.primaryID
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
columnFieldID = defaults.String(m.fkID, fmt.Sprintf("%s%s", flect.Underscore(t.Name()), "_id"))

subQuery := fmt.Sprintf("select %s from %s where %s = ?", columnFieldID, m.manyToManyTableName, modelColumnID)
modelIDValue := m.model.FieldByName("ID").Interface()
Expand Down
17 changes: 17 additions & 0 deletions finders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,20 @@ func Test_Exists(t *testing.T) {
r.True(t)
})
}

func Test_FindManyToMany(t *testing.T) {
transaction(func(tx *Connection) {
r := require.New(t)
parent := &Parent{}
r.NoError(tx.Create(parent))

student := &Student{}
r.NoError(tx.Create(student))

r.NoError(tx.RawQuery("INSERT INTO parents_students (student_id, parent_id) VALUES(?,?)", student.ID, parent.ID).Exec())

p := &Parent{}
err := tx.Eager("Students").Find(p, parent.ID)
r.NoError(err)
})
}
1 change: 1 addition & 0 deletions migrations/20190219210052_students.down.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop_table("students")
3 changes: 3 additions & 0 deletions migrations/20190219210052_students.up.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
create_table("students") {
t.Column("id", "uuid", {"primary": true})
}
1 change: 1 addition & 0 deletions migrations/20190219210059_parents.down.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop_table("parents")
3 changes: 3 additions & 0 deletions migrations/20190219210059_parents.up.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
create_table("parents") {
t.Column("id", "uuid", {"primary": true})
}
1 change: 1 addition & 0 deletions migrations/20190219211041_parents_students.down.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop_table("parents_students")
5 changes: 5 additions & 0 deletions migrations/20190219211041_parents_students.up.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create_table("parents_students") {
t.Column("parent_id", "uuid", {})
t.Column("student_id", "uuid", {})
t.DisableTimestamps()
}
14 changes: 14 additions & 0 deletions pop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,17 @@ type HeadPtr struct {
BodyID *int `json:"-" db:"body_id"`
Body *Body `json:"body,omitempty" belongs_to:"body"`
}

type Student struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}

// https://github.com/gobuffalo/pop/issues/302
type Parent struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Students []*Student `many_to_many:"parents_students"`
}

0 comments on commit 4321166

Please sign in to comment.