Finish ToOneSet
This commit is contained in:
parent
c1962eb7ac
commit
9db43ae3ad
2 changed files with 28 additions and 13 deletions
|
@ -4,14 +4,28 @@
|
||||||
// Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
|
// Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item.
|
||||||
// Sets R.{{.Function.Name}} to related.
|
// Sets R.{{.Function.Name}} to related.
|
||||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec boil.Executor, insert bool, related *{{.ForeignTable.NameGo}}) error {
|
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec boil.Executor, insert bool, related *{{.ForeignTable.NameGo}}) error {
|
||||||
//{{.Function.Receiver}}.R.{{.Function.Name}} = related
|
var err error
|
||||||
//{{.Function.Receiver}}.R.{{.Function.Name}}.{{.Function.ForeignAssignment}} = {{.Function.Receiver}}.{{.Function.LocalAssignment}}
|
if insert {
|
||||||
//if insert {
|
if err = related.Insert(exec); err != nil {
|
||||||
// return related.Insert()
|
return err
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// return exec.Exec(`update "{{.ForeignKey.Table}}" set "{{.ForeignKey.Column}}" = $1`, 5)
|
_, err = exec.Exec(`update "{{.ForeignKey.Table}}" set "{{.ForeignKey.Column}}" = $1`, related.{{.Function.ForeignAssignment}})
|
||||||
return nil
|
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
|
||||||
}
|
}
|
||||||
{{- if .ForeignKey.Nullable}}
|
{{- if .ForeignKey.Nullable}}
|
||||||
|
|
||||||
|
|
|
@ -9,11 +9,10 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
||||||
var a {{.LocalTable.NameGo}}
|
var a {{.LocalTable.NameGo}}
|
||||||
var b, c {{.ForeignTable.NameGo}}
|
var b, c {{.ForeignTable.NameGo}}
|
||||||
|
|
||||||
if err := a.Insert(tx); err != nil {
|
seed := randomize.NewSeed()
|
||||||
|
if err = randomize.Struct(seed, &a, {{.ForeignKey.Table | singular | camelCase}}DBTypes, false); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
seed := randomize.NewSeed()
|
|
||||||
if err = randomize.Struct(seed, &b, {{$varNameSingular}}DBTypes, false); err != nil {
|
if err = randomize.Struct(seed, &b, {{$varNameSingular}}DBTypes, false); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -21,12 +20,14 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := a.Insert(tx); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
if err = b.Insert(tx); err != nil {
|
if err = b.Insert(tx); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, x := range []*{{.ForeignTable.NameGo}}{&b, &c} {
|
for i, x := range []*{{.ForeignTable.NameGo}}{&b, &c} {
|
||||||
t.Log(i)
|
|
||||||
err = a.Set{{.Function.Name}}(tx, i != 0, x)
|
err = a.Set{{.Function.Name}}(tx, i != 0, x)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -40,14 +41,14 @@ func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Na
|
||||||
}
|
}
|
||||||
|
|
||||||
zero := reflect.Zero(reflect.TypeOf(a.{{.Function.LocalAssignment}}))
|
zero := reflect.Zero(reflect.TypeOf(a.{{.Function.LocalAssignment}}))
|
||||||
reflect.ValueOf(a.{{.Function.LocalAssignment}}).Set(zero)
|
reflect.Indirect(reflect.ValueOf(&a.{{.Function.LocalAssignment}})).Set(zero)
|
||||||
|
|
||||||
if err = a.Reload(tx); err != nil {
|
if err = a.Reload(tx); err != nil {
|
||||||
t.Fatal("failed to reload", err)
|
t.Fatal("failed to reload", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
if a.{{.Function.LocalAssignment}} != x.{{.Function.ForeignAssignment}} {
|
||||||
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}})
|
t.Error("foreign key was wrong value", a.{{.Function.LocalAssignment}}, x.{{.Function.ForeignAssignment}})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue