Complete removal on the remove method
This commit is contained in:
parent
947081c579
commit
0eea12fb75
2 changed files with 37 additions and 3 deletions
|
@ -54,7 +54,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Set{{.Function.Name}}(exec
|
||||||
|
|
||||||
// Remove{{.Function.Name}} relationship.
|
// Remove{{.Function.Name}} relationship.
|
||||||
// Sets {{.Function.Receiver}}.R.{{.Function.Name}} to nil.
|
// Sets {{.Function.Receiver}}.R.{{.Function.Name}} to nil.
|
||||||
// Removes {{.Function.Receiver}} from all passed in related items' relationships struct.
|
// Removes {{.Function.Receiver}} from all passed in related items' relationships struct (Optional).
|
||||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(exec boil.Executor, related ...*{{.ForeignTable.NameGo}}) error {
|
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(exec boil.Executor, related ...*{{.ForeignTable.NameGo}}) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -64,6 +64,30 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) Remove{{.Function.Name}}(e
|
||||||
return errors.Wrap(err, "failed to update local table")
|
return errors.Wrap(err, "failed to update local table")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, rel := range related {
|
||||||
|
if rel.R == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
{{if .ForeignKey.Unique -}}
|
||||||
|
rel.R.{{.Function.ForeignName}} = nil
|
||||||
|
{{else -}}
|
||||||
|
for i, ri := range rel.R.{{.Function.ForeignName}} {
|
||||||
|
if {{.Function.Receiver}}.{{.Function.LocalAssignment}} != ri.{{.Function.LocalAssignment}} {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
ln := len(rel.R.{{.Function.ForeignName}})
|
||||||
|
if ln > 1 && i < ln-1 {
|
||||||
|
rel.R.{{.Function.ForeignName}}[i], rel.R.{{.Function.ForeignName}}[ln-1] =
|
||||||
|
rel.R.{{.Function.ForeignName}}[ln-1], rel.R.{{.Function.ForeignName}}[i]
|
||||||
|
}
|
||||||
|
rel.R.{{.Function.ForeignName}} = rel.R.{{.Function.ForeignName}}[:ln-1]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
{{end -}}
|
||||||
|
}
|
||||||
|
|
||||||
{{.Function.Receiver}}.R.{{.Function.Name}} = nil
|
{{.Function.Receiver}}.R.{{.Function.Name}} = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,7 +90,7 @@ func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = a.Remove{{.Function.Name}}(tx); err != nil {
|
if err = a.Remove{{.Function.Name}}(tx, &b); err != nil {
|
||||||
t.Error("failed to remove relationship")
|
t.Error("failed to remove relationship")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,9 +106,19 @@ func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function
|
||||||
t.Error("R struct entry should be nil")
|
t.Error("R struct entry should be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
if a.{{.ForeignKey.Column | titleCase}}.Valid {
|
if a.{{.LocalTable.ColumnNameGo}}.Valid {
|
||||||
t.Error("R struct entry should be nil")
|
t.Error("R struct entry should be nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{{if .ForeignKey.Unique -}}
|
||||||
|
if b.R.{{.Function.ForeignName}} != nil {
|
||||||
|
t.Error("failed to remove a from b's relationships")
|
||||||
|
}
|
||||||
|
{{else -}}
|
||||||
|
if len(b.R.{{.Function.ForeignName}}) != 0 {
|
||||||
|
t.Error("failed to remove a from b's relationships")
|
||||||
|
}
|
||||||
|
{{end -}}
|
||||||
}
|
}
|
||||||
{{end -}}
|
{{end -}}
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue