diff --git a/templates.go b/templates.go index 9d7eaba..c917070 100644 --- a/templates.go +++ b/templates.go @@ -110,6 +110,7 @@ var templateFunctions = template.FuncMap{ "replace": func(rep, with, str string) string { return strings.Replace(str, rep, with, -1) }, "prefix": func(add, str string) string { return fmt.Sprintf("%s%s", add, str) }, "quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) }, + "id": strmangle.Identifier, // Pluralization "singular": strmangle.Singular, diff --git a/templates/05_relationship_to_many.tpl b/templates/05_relationship_to_many.tpl index fc4d046..ea0fe25 100644 --- a/templates/05_relationship_to_many.tpl +++ b/templates/05_relationship_to_many.tpl @@ -7,16 +7,14 @@ {{- $colName := $localTableSing | printf "%s_id" -}} {{- $receiver := .Table.Name | toLower | substring 0 1 -}} {{- range toManyRelationships .Table.Name .Tables -}} - {{- if .ToJoinTable -}} - {{- else -}} {{- $foreignTableSing := .ForeignTable | singular}} {{- $foreignTable := $foreignTableSing | titleCase}} {{- $foreignSlice := $foreignTableSing | camelCase | printf "%sSlice"}} {{- $foreignTableHumanReadable := .ForeignTable | replace "_" " " -}} {{- $foreignPluralNoun := .ForeignTable | plural | titleCase -}} - {{- $isNormal := eq $colName .ForeignColumn -}} + {{- $isForeignKeySimplyTableName := or (eq $colName .ForeignColumn) .ToJoinTable -}} - {{- if $isNormal -}} + {{- if $isForeignKeySimplyTableName -}} // {{$foreignPluralNoun}} retrieves all the {{$localTableSing}}'s {{$foreignTableHumanReadable}}. func ({{$receiver}} *{{$localTable}}) {{$foreignPluralNoun}}( @@ -29,7 +27,11 @@ func ({{$receiver}} *{{$localTable}}) {{$fnName}}( exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) { var ret {{$foreignSlice}} + {{if .ToJoinTable -}} + query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} "{{id 0}}" inner join {{.JoinTable}} as "{{id 1}}" on "{{id 1}}"."{{.JoinForeignColumn}}" = "{{id 0}}"."{{.ForeignColumn}}" where "{{id 1}}"."{{.JoinLocalColumn}}"=$1`, `"{{id 1}}".` + strings.Join(selectCols, `","{{id 0}}"."`)) + {{else -}} query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where "{{.ForeignColumn}}"=$1`, strings.Join(selectCols, `","`)) + {{end}} rows, err := exec.Query(query, {{.Column | titleCase | printf "%s.%s" $receiver }}) if err != nil { return nil, fmt.Errorf(`{{$dot.PkgName}}: unable to select from {{.ForeignTable}}: %v`, err) @@ -53,6 +55,5 @@ exec boil.Executor, selectCols ...string) ({{$foreignSlice}}, error) { return ret, nil } -{{end -}}{{/* if join table */}} -{{- end -}}{{/* range relationships */}} -{{- end -}}{{/* outer if join table */}} +{{end -}}{{- /* range relationships */ -}} +{{- end -}}{{- /* outer if join table */ -}}