Fix to_many relationship bug.

- IDs were being inserted incorrectly causing an sql key duplicate error
  instead of being inserted into the join table.
This commit is contained in:
Aaron L 2016-07-14 23:28:33 -07:00
parent e6b8cfff93
commit bbf40bea53
2 changed files with 21 additions and 5 deletions

View file

@ -15,7 +15,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}X(exec boil.Executor, selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) {
var ret {{$rel.ForeignTable.Slice}}
selectColumns := `*`
selectColumns := `"{{id 0}}".*`
if len(selectCols) != 0 {
selectColumns = `"{{id 0}}".` + strings.Join(selectCols, `","{{id 0}}"."`)
}

View file

@ -16,19 +16,35 @@ func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
t.Fatal(err)
}
boil.RandomizeStruct(&b, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, true)
boil.RandomizeStruct(&c, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, true)
boil.RandomizeStruct(&b, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, true, "{{.ForeignColumn}}")
boil.RandomizeStruct(&c, {{$rel.ForeignTable.NameSingular | camelCase}}DBTypes, true, "{{.ForeignColumn}}")
{{if not .ToJoinTable -}}
b.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}}
c.{{$rel.Function.ForeignAssignment}} = a.{{$rel.Function.LocalAssignment}}
if err := b.InsertX(tx); err != nil {
{{end -}}
if err = b.InsertX(tx); err != nil {
t.Fatal(err)
}
if err := c.InsertX(tx); err != nil {
if err = c.InsertX(tx); err != nil {
t.Fatal(err)
}
{{if .ToJoinTable -}}
_, err = tx.Exec(`insert into {{.JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{.Column | titleCase}}, b.{{.ForeignColumn | titleCase}})
if err != nil {
t.Fatal(err)
}
_, err = tx.Exec(`insert into {{.JoinTable}} ({{.JoinLocalColumn}}, {{.JoinForeignColumn}}) values ($1, $2)`, a.{{.Column | titleCase}}, c.{{.ForeignColumn | titleCase}})
if err != nil {
t.Fatal(err)
}
{{end}}
{{$varname := $rel.ForeignTable.NamePluralGo | toLower -}}
{{$varname}}, err := a.{{$rel.Function.Name}}X(tx)
if err != nil {
t.Fatal(err)
}
bFound, cFound := false, false
for _, v := range {{$varname}} {