2016-06-20 01:00:37 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
|
|
|
{{- $pkg := .PkgName -}}
|
2016-06-20 07:22:50 +02:00
|
|
|
{{- $localTable := .Table.Name | singular | titleCase -}}
|
2016-06-20 01:00:37 +02:00
|
|
|
{{- range .Table.FKeys -}}
|
2016-06-20 07:22:50 +02:00
|
|
|
{{- $localColumn := .Column | remove "_id" | singular | titleCase -}}
|
|
|
|
{{- $foreignColumn := .Column | remove "_id" | singular | titleCase -}}
|
|
|
|
{{- $foreignTable := .ForeignTable | singular | titleCase -}}
|
|
|
|
{{- $varname := .ForeignTable | singular | camelCase -}}
|
|
|
|
{{- $receiver := $localTable | toLower | substring 0 1 -}}
|
2016-06-20 01:00:37 +02:00
|
|
|
// {{$foreignColumn}} fetches the {{$foreignTable}} pointed to by the foreign key.
|
|
|
|
func ({{$receiver}} *{{$localTable}}) {{$foreignColumn}}(exec boil.Executor, selectCols ...string) (*{{$foreignTable}}, error) {
|
|
|
|
{{$varname}} := &{{$foreignTable}}{}
|
|
|
|
|
2016-06-26 05:19:13 +02:00
|
|
|
query := fmt.Sprintf(`select "%s" from {{.ForeignTable}} where id = $1`, strings.Join(selectCols, `","`))
|
2016-06-20 01:00:37 +02:00
|
|
|
err := exec.QueryRow(query, {{$receiver}}.{{titleCase .Column}}).Scan(boil.GetStructPointers({{$varname}}, selectCols...)...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`{{$pkg}}: unable to select from {{.ForeignTable}}: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {{$varname}}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
{{end -}}
|
|
|
|
{{- end -}}
|