diff --git a/templates/04_relationship_to_one.tpl b/templates/04_relationship_to_one.tpl index 7303b4a..dd4f60e 100644 --- a/templates/04_relationship_to_one.tpl +++ b/templates/04_relationship_to_one.tpl @@ -48,6 +48,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}X(exec b {{- $dot := . -}} {{- range .Table.FKeys -}} {{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}} +// TO_ONE {{- template "relationship_to_one_helper" $rel -}} {{end -}} {{- end -}} diff --git a/templates/05_relationship_to_many.tpl b/templates/05_relationship_to_many.tpl index 28debc6..7bdbe93 100644 --- a/templates/05_relationship_to_many.tpl +++ b/templates/05_relationship_to_many.tpl @@ -3,6 +3,9 @@ {{- $dot := . }} {{- $table := .Table }} {{- range .Table.ToManyRelationships -}} + {{- if .ForeignColumnUnique -}} +{{- template "relationship_to_one_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}} + {{- else -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}} // {{$rel.Function.Name}} retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. @@ -21,6 +24,17 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na return o } +// {{$rel.Function.Name}}XP panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor +{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. +func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}XP(exec boil.Executor, selectCols ...string) {{$rel.ForeignTable.Slice}} { + o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}X(exec, selectCols...) + if err != nil { + panic(boil.WrapErr(err)) + } + + return o +} + // {{$rel.Function.Name}}X retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor {{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}X(exec boil.Executor, selectCols ...string) ({{$rel.ForeignTable.Slice}}, error) { @@ -59,15 +73,6 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na return ret, nil } -// {{$rel.Function.Name}}XP panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor -{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}. -func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}XP(exec boil.Executor, selectCols ...string) {{$rel.ForeignTable.Slice}} { - o, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}X(exec, selectCols...) - if err != nil { - panic(boil.WrapErr(err)) - } - - return o -} -{{end -}}{{- /* range relationships */ -}} +{{end -}}{{- /* if unique foreign key */ -}} +{{- end -}}{{- /* range relationships */ -}} {{- end -}}{{- /* outer if join table */ -}} diff --git a/templates_test/relationship_to_many.tpl b/templates_test/relationship_to_many.tpl index 5848a0a..a59646a 100644 --- a/templates_test/relationship_to_many.tpl +++ b/templates_test/relationship_to_many.tpl @@ -3,6 +3,9 @@ {{- $dot := . }} {{- $table := .Table }} {{- range .Table.ToManyRelationships -}} + {{- if .ForeignColumnUnique -}} +{{- template "relationship_to_one_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}} + {{- else -}} {{- $rel := textsFromRelationship $dot.Tables $table . -}} func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { var err error @@ -75,5 +78,6 @@ func Test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) { } } -{{end -}}{{- /* range */ -}} +{{end -}}{{- /* if unique */ -}} +{{- end -}}{{- /* range */ -}} {{- end -}}{{- /* outer if join table */ -}} diff --git a/templates_test/relationship_to_one.tpl b/templates_test/relationship_to_one.tpl index 48ea4e2..749027f 100644 --- a/templates_test/relationship_to_one.tpl +++ b/templates_test/relationship_to_one.tpl @@ -12,6 +12,8 @@ func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}( foreign.{{.ForeignKey.ForeignColumn | titleCase}}.Valid = true {{end}} + + {{if not .Function.ReverseArgs -}} if err := foreign.InsertX(tx); err != nil { t.Fatal(err) } @@ -20,14 +22,24 @@ func Test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}( if err := local.InsertX(tx); err != nil { t.Fatal(err) } + {{else -}} + if err := local.InsertX(tx); err != nil { + t.Fatal(err) + } - checkForeign, err := local.{{.Function.Name}}X(tx) + foreign.{{.Function.ForeignAssignment}} = local.{{.Function.LocalAssignment}} + if err := foreign.InsertX(tx); err != nil { + t.Fatal(err) + } + {{end -}} + + check, err := local.{{.Function.Name}}X(tx) if err != nil { t.Fatal(err) } - if checkForeign.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} { - t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, checkForeign.{{.Function.ForeignAssignment}}) + if check.{{.Function.ForeignAssignment}} != foreign.{{.Function.ForeignAssignment}} { + t.Errorf("want: %v, got %v", foreign.{{.Function.ForeignAssignment}}, check.{{.Function.ForeignAssignment}}) } }