2016-08-26 08:50:45 +02:00
|
|
|
{{- define "relationship_to_one_setops_helper" -}}
|
2016-08-26 16:59:28 +02:00
|
|
|
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase}}
|
|
|
|
|
2016-08-26 08:50:45 +02:00
|
|
|
// Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
|
|
|
|
// Sets R.{{.Function.Name}} to related.
|
2016-08-27 07:04:49 +02:00
|
|
|
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec boil.Executor, insert bool, related *{{.ForeignTable.NameGo}}) error {
|
2016-08-27 07:43:50 +02:00
|
|
|
var err error
|
|
|
|
if insert {
|
|
|
|
if err = related.Insert(exec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
|
2016-08-27 07:43:50 +02:00
|
|
|
_, err = exec.Exec(`update "{{.ForeignKey.Table}}" set "{{.ForeignKey.Column}}" = $1`, related.{{.Function.ForeignAssignment}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if {{.Function.Receiver}}.R == nil {
|
|
|
|
{{.Function.Receiver}}.R = &{{.LocalTable.NameGo}}R{
|
|
|
|
{{.Function.Name}}: related,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
{{.Function.Receiver}}.R.{{.Function.Name}} = related
|
|
|
|
}
|
|
|
|
|
|
|
|
{{.Function.Receiver}}.{{.Function.LocalAssignment}} = related.{{.Function.ForeignAssignment}}
|
|
|
|
return nil
|
2016-08-26 08:50:45 +02:00
|
|
|
}
|
2016-08-26 16:59:28 +02:00
|
|
|
{{- if .ForeignKey.Nullable}}
|
2016-08-26 08:50:45 +02:00
|
|
|
|
|
|
|
// Remove{{.Function.Name}} relationship.
|
|
|
|
// Sets R.{{.Function.Name}} to nil.
|
2016-08-27 07:04:49 +02:00
|
|
|
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(exec boil.Executor) error {
|
2016-08-26 08:50:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
{{end -}}
|
2016-08-26 16:59:28 +02:00
|
|
|
{{- end -}}
|
2016-08-26 08:50:45 +02:00
|
|
|
{{- if .Table.IsJoinTable -}}
|
|
|
|
{{- else -}}
|
|
|
|
{{- $dot := . -}}
|
|
|
|
{{- range .Table.FKeys -}}
|
|
|
|
{{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table . -}}
|
|
|
|
{{- template "relationship_to_one_setops_helper" $rel -}}
|
|
|
|
{{- end -}}
|
|
|
|
{{- end -}}
|