Fix SetOp for half join tables
This commit is contained in:
parent
6401a277cf
commit
fa2202abb2
2 changed files with 30 additions and 13 deletions
|
@ -3,8 +3,9 @@
|
|||
{{- $dot := . -}}
|
||||
{{- range .Table.FKeys -}}
|
||||
{{- $txt := txtsFromFKey $dot.Tables $dot.Table . -}}
|
||||
{{- $varNameSingular := .ForeignTable | singular | camelCase -}}
|
||||
{{- $localNameSingular := .Table | singular | camelCase}}
|
||||
{{- $foreignNameSingular := .ForeignTable | singular | camelCase -}}
|
||||
{{- $varNameSingular := .Table | singular | camelCase}}
|
||||
{{- $schemaTable := .Table | $dot.SchemaTable -}}
|
||||
// Set{{$txt.Function.Name}} of the {{.Table | singular}} to the related item.
|
||||
// Sets o.R.{{$txt.Function.Name}} to related.
|
||||
// Adds o to related.R.{{$txt.Function.ForeignName}}.
|
||||
|
@ -16,21 +17,29 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo
|
|||
}
|
||||
}
|
||||
|
||||
oldVal := o.{{$txt.Function.LocalAssignment}}
|
||||
updateQuery := fmt.Sprintf(
|
||||
"UPDATE {{$schemaTable}} SET %s WHERE %s",
|
||||
strmangle.SetParamNames("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}1{{else}}0{{end}}, []string{{"{"}}"{{.Column}}"{{"}"}}),
|
||||
strmangle.WhereClause("{{$dot.LQ}}", "{{$dot.RQ}}", {{if $dot.Dialect.IndexPlaceholders}}2{{else}}0{{end}}, {{$varNameSingular}}PrimaryKeyColumns),
|
||||
)
|
||||
values := []interface{}{related.{{$txt.ForeignTable.ColumnNameGo}}, o.{{$dot.Table.PKey.Columns | stringMap $dot.StringFuncs.titleCase | join ", o."}}{{"}"}}
|
||||
|
||||
if boil.DebugMode {
|
||||
fmt.Fprintln(boil.DebugWriter, updateQuery)
|
||||
fmt.Fprintln(boil.DebugWriter, values)
|
||||
}
|
||||
|
||||
if _, err = exec.Exec(updateQuery, values...); err != nil {
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
|
||||
o.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}}
|
||||
{{if .Nullable -}}
|
||||
o.{{$txt.LocalTable.ColumnNameGo}}.Valid = true
|
||||
{{- end}}
|
||||
if err = o.Update(exec, "{{.Column}}"); err != nil {
|
||||
o.{{$txt.Function.LocalAssignment}} = oldVal
|
||||
{{if .Nullable -}}
|
||||
o.{{$txt.LocalTable.ColumnNameGo}}.Valid = false
|
||||
{{- end}}
|
||||
return errors.Wrap(err, "failed to update local table")
|
||||
}
|
||||
|
||||
if o.R == nil {
|
||||
o.R = &{{$localNameSingular}}R{
|
||||
o.R = &{{$varNameSingular}}R{
|
||||
{{$txt.Function.Name}}: related,
|
||||
}
|
||||
} else {
|
||||
|
@ -39,7 +48,7 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo
|
|||
|
||||
{{if .Unique -}}
|
||||
if related.R == nil {
|
||||
related.R = &{{$varNameSingular}}R{
|
||||
related.R = &{{$foreignNameSingular}}R{
|
||||
{{$txt.Function.ForeignName}}: o,
|
||||
}
|
||||
} else {
|
||||
|
@ -47,7 +56,7 @@ func (o *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executo
|
|||
}
|
||||
{{else -}}
|
||||
if related.R == nil {
|
||||
related.R = &{{$varNameSingular}}R{
|
||||
related.R = &{{$foreignNameSingular}}R{
|
||||
{{$txt.Function.ForeignName}}: {{$txt.LocalTable.NameGo}}Slice{{"{"}}o{{"}"}},
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -60,6 +60,13 @@ func test{{$txt.LocalTable.NameGo}}ToOneSetOp{{$txt.ForeignTable.NameGo}}Using{{
|
|||
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}})
|
||||
}
|
||||
|
||||
{{if setInclude .Column $dot.Table.PKey.Columns -}}
|
||||
if exists, err := {{$txt.LocalTable.NameGo}}Exists(tx, a.{{$dot.Table.PKey.Columns | stringMap $dot.StringFuncs.titleCase | join ", a."}}); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if !exists {
|
||||
t.Error("want 'a' to exist")
|
||||
}
|
||||
{{else -}}
|
||||
zero := reflect.Zero(reflect.TypeOf(a.{{$txt.Function.LocalAssignment}}))
|
||||
reflect.Indirect(reflect.ValueOf(&a.{{$txt.Function.LocalAssignment}})).Set(zero)
|
||||
|
||||
|
@ -74,6 +81,7 @@ func test{{$txt.LocalTable.NameGo}}ToOneSetOp{{$txt.ForeignTable.NameGo}}Using{{
|
|||
{{end -}}
|
||||
t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}})
|
||||
}
|
||||
{{- end}}
|
||||
}
|
||||
}
|
||||
{{- if .Nullable}}
|
||||
|
|
Loading…
Reference in a new issue