Make to_one relationships use the X paradigm.
This commit is contained in:
parent
d7adb7006e
commit
2e3830053d
1 changed files with 9 additions and 4 deletions
|
@ -4,15 +4,20 @@
|
||||||
{{- $localTable := .Table.Name | singular | titleCase -}}
|
{{- $localTable := .Table.Name | singular | titleCase -}}
|
||||||
{{- range .Table.FKeys -}}
|
{{- range .Table.FKeys -}}
|
||||||
{{- $localColumn := .Column | remove "_id" | singular | titleCase -}}
|
{{- $localColumn := .Column | remove "_id" | singular | titleCase -}}
|
||||||
{{- $foreignColumn := .Column | remove "_id" | singular | titleCase -}}
|
{{- $foreignColumn := .ForeignColumn | remove "_id" | singular | titleCase -}}
|
||||||
{{- $foreignTable := .ForeignTable | singular | titleCase -}}
|
{{- $foreignTable := .ForeignTable | singular | titleCase -}}
|
||||||
{{- $varname := .ForeignTable | singular | camelCase -}}
|
{{- $varname := .ForeignTable | singular | camelCase -}}
|
||||||
{{- $receiver := $localTable | toLower | substring 0 1 -}}
|
{{- $receiver := $localTable | toLower | substring 0 1 -}}
|
||||||
// {{$foreignColumn}} fetches the {{$foreignTable}} pointed to by the foreign key.
|
// {{$localColumn}} fetches the {{$foreignTable}} pointed to by the foreign key.
|
||||||
func ({{$receiver}} *{{$localTable}}) {{$foreignColumn}}(exec boil.Executor, selectCols ...string) (*{{$foreignTable}}, error) {
|
func ({{$receiver}} *{{$localTable}}) {{$localColumn}}(selectCols ...string) (*{{$foreignTable}}, error) {
|
||||||
|
return {{$receiver}}.{{$localColumn}}X(boil.GetDB(), selectCols...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// {{$localColumn}} fetches the {{$foreignTable}} pointed to by the foreign key.
|
||||||
|
func ({{$receiver}} *{{$localTable}}) {{$localColumn}}X(exec boil.Executor, selectCols ...string) (*{{$foreignTable}}, error) {
|
||||||
{{$varname}} := &{{$foreignTable}}{}
|
{{$varname}} := &{{$foreignTable}}{}
|
||||||
|
|
||||||
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where id = $1`, strings.Join(selectCols, `","`))
|
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where {{.Column}} = $1`, strings.Join(selectCols, `","`))
|
||||||
err := exec.QueryRow(query, {{$receiver}}.{{titleCase .Column}}).Scan(boil.GetStructPointers({{$varname}}, selectCols...)...)
|
err := exec.QueryRow(query, {{$receiver}}.{{titleCase .Column}}).Scan(boil.GetStructPointers({{$varname}}, selectCols...)...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf(`{{$pkg}}: unable to select from {{.ForeignTable}}: %v`, err)
|
return nil, fmt.Errorf(`{{$pkg}}: unable to select from {{.ForeignTable}}: %v`, err)
|
||||||
|
|
Loading…
Reference in a new issue