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:
parent
e6b8cfff93
commit
bbf40bea53
2 changed files with 21 additions and 5 deletions
|
@ -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}}"."`)
|
||||
}
|
||||
|
|
|
@ -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}} {
|
||||
|
|
Loading…
Reference in a new issue