diff --git a/templates/10_relationship_to_one_setops.tpl b/templates/10_relationship_to_one_setops.tpl index d2cbeaa..7d96e5f 100644 --- a/templates/10_relationship_to_one_setops.tpl +++ b/templates/10_relationship_to_one_setops.tpl @@ -18,10 +18,15 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function } oldVal := {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} - related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}} + {{if .Nullable -}} + {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true + {{- end}} if err = {{$txt.Function.Receiver}}.Update(exec, "{{.Column}}"); err != nil { {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = oldVal + {{if .Nullable -}} + {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = false + {{- end}} return errors.Wrap(err, "failed to update local table") } @@ -49,11 +54,8 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function } else { related.R.{{$txt.Function.ForeignName}} = append(related.R.{{$txt.Function.ForeignName}}, {{$txt.Function.Receiver}}) } - {{end -}} + {{- end}} - {{if .Nullable}} - {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true - {{end -}} return nil } diff --git a/templates/11_relationship_one_to_one_setops.tpl b/templates/11_relationship_one_to_one_setops.tpl index bc7148b..72c862c 100644 --- a/templates/11_relationship_one_to_one_setops.tpl +++ b/templates/11_relationship_one_to_one_setops.tpl @@ -10,18 +10,29 @@ // Adds {{$txt.Function.Receiver}} to related.R.{{$txt.Function.ForeignName}}. func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function.Name}}(exec boil.Executor, insert bool, related *{{$txt.ForeignTable.NameGo}}) error { var err error + + oldVal := related.{{$txt.Function.ForeignAssignment}} + related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} + {{if .ForeignColumnNullable -}} + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true + {{- end}} + if insert { if err = related.Insert(exec); err != nil { + related.{{$txt.Function.ForeignAssignment}} = oldVal + {{if .ForeignColumnNullable -}} + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false + {{- end}} return errors.Wrap(err, "failed to insert into foreign table") } - } - - oldVal := {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} - related.{{$txt.Function.ForeignAssignment}} = {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} - {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = related.{{$txt.Function.ForeignAssignment}} - if err = {{$txt.Function.Receiver}}.Update(exec, "{{.Column}}"); err != nil { - {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} = oldVal - return errors.Wrap(err, "failed to update local table") + } else { + if err = related.Update(exec, "{{.ForeignColumn}}"); err != nil { + related.{{$txt.Function.ForeignAssignment}} = oldVal + {{if .ForeignColumnNullable -}} + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false + {{- end}} + return errors.Wrap(err, "failed to update foreign table") + } } if {{$txt.Function.Receiver}}.R == nil { @@ -32,7 +43,6 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} = related } - {{if .Unique -}} if related.R == nil { related.R = &{{$varNameSingular}}R{ {{$txt.Function.ForeignName}}: {{$txt.Function.Receiver}}, @@ -40,32 +50,19 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Set{{$txt.Function } else { related.R.{{$txt.Function.ForeignName}} = {{$txt.Function.Receiver}} } - {{else -}} - if related.R == nil { - related.R = &{{$varNameSingular}}R{ - {{$txt.Function.ForeignName}}: {{$txt.LocalTable.NameGo}}Slice{{"{"}}{{$txt.Function.Receiver}}{{"}"}}, - } - } else { - related.R.{{$txt.Function.ForeignName}} = append(related.R.{{$txt.Function.ForeignName}}, {{$txt.Function.Receiver}}) - } - {{end -}} - - {{if .Nullable}} - {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true - {{end -}} return nil } - {{- if .Nullable}} + {{- if .ForeignColumnNullable}} // Remove{{$txt.Function.Name}} relationship. // Sets {{$txt.Function.Receiver}}.R.{{$txt.Function.Name}} to nil. // Removes {{$txt.Function.Receiver}} from all passed in related items' relationships struct (Optional). func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Function.Name}}(exec boil.Executor, related *{{$txt.ForeignTable.NameGo}}) error { var err error - {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = false - if err = {{$txt.Function.Receiver}}.Update(exec, "{{.Column}}"); err != nil { - {{$txt.Function.Receiver}}.{{$txt.LocalTable.ColumnNameGo}}.Valid = true + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = false + if err = related.Update(exec, "{{.ForeignColumn}}"); err != nil { + related.{{$txt.ForeignTable.ColumnNameGo}}.Valid = true return errors.Wrap(err, "failed to update local table") } @@ -74,27 +71,7 @@ func ({{$txt.Function.Receiver}} *{{$txt.LocalTable.NameGo}}) Remove{{$txt.Funct return nil } - {{if .Unique -}} related.R.{{$txt.Function.ForeignName}} = nil - {{else -}} - for i, ri := range related.R.{{$txt.Function.ForeignName}} { - {{if $txt.Function.UsesBytes -}} - if 0 != bytes.Compare({{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}}, ri.{{$txt.Function.LocalAssignment}}) { - {{else -}} - if {{$txt.Function.Receiver}}.{{$txt.Function.LocalAssignment}} != ri.{{$txt.Function.LocalAssignment}} { - {{end -}} - continue - } - - ln := len(related.R.{{$txt.Function.ForeignName}}) - if ln > 1 && i < ln-1 { - related.R.{{$txt.Function.ForeignName}}[i] = related.R.{{$txt.Function.ForeignName}}[ln-1] - } - related.R.{{$txt.Function.ForeignName}} = related.R.{{$txt.Function.ForeignName}}[:ln-1] - break - } - {{end -}} - return nil } {{end -}}{{/* if foreignkey nullable */}} diff --git a/templates_test/relationship_one_to_one_setops.tpl b/templates_test/relationship_one_to_one_setops.tpl index 3c6fdac..b69252f 100644 --- a/templates_test/relationship_one_to_one_setops.tpl +++ b/templates_test/relationship_one_to_one_setops.tpl @@ -38,6 +38,13 @@ func test{{$txt.LocalTable.NameGo}}OneToOneSetOp{{$txt.ForeignTable.NameGo}}_{{$ t.Fatal(err) } + if a.R.{{$txt.Function.Name}} != x { + t.Error("relationship struct not set to correct value") + } + if x.R.{{$txt.Function.ForeignName}} != &a { + t.Error("failed to append to foreign relationship struct") + } + {{if $txt.Function.UsesBytes -}} if 0 != bytes.Compare(a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) { {{else -}} @@ -45,14 +52,11 @@ func test{{$txt.LocalTable.NameGo}}OneToOneSetOp{{$txt.ForeignTable.NameGo}}_{{$ {{end -}} t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}) } - if a.R.{{$txt.Function.Name}} != x { - t.Error("relationship struct not set to correct value") - } - zero := reflect.Zero(reflect.TypeOf(a.{{$txt.Function.LocalAssignment}})) - reflect.Indirect(reflect.ValueOf(&a.{{$txt.Function.LocalAssignment}})).Set(zero) + zero := reflect.Zero(reflect.TypeOf(x.{{$txt.Function.ForeignAssignment}})) + reflect.Indirect(reflect.ValueOf(&x.{{$txt.Function.ForeignAssignment}})).Set(zero) - if err = a.Reload(tx); err != nil { + if err = x.Reload(tx); err != nil { t.Fatal("failed to reload", err) } @@ -64,15 +68,9 @@ func test{{$txt.LocalTable.NameGo}}OneToOneSetOp{{$txt.ForeignTable.NameGo}}_{{$ t.Error("foreign key was wrong value", a.{{$txt.Function.LocalAssignment}}, x.{{$txt.Function.ForeignAssignment}}) } - {{if .Unique -}} - if x.R.{{$txt.Function.ForeignName}} != &a { - t.Error("failed to append to foreign relationship struct") + if err = x.Delete(tx); err != nil { + t.Fatal("failed to delete x", err) } - {{else -}} - if x.R.{{$txt.Function.ForeignName}}[0] != &a { - t.Error("failed to append to foreign relationship struct") - } - {{end -}} } } {{- if .ForeignColumnNullable}} @@ -118,19 +116,13 @@ func test{{$txt.LocalTable.NameGo}}OneToOneRemoveOp{{$txt.ForeignTable.NameGo}}_ t.Error("R struct entry should be nil") } - if a.{{$txt.LocalTable.ColumnNameGo}}.Valid { - t.Error("R struct entry should be nil") + if b.{{$txt.ForeignTable.ColumnNameGo}}.Valid { + t.Error("foreign key column should be nil") } - {{if .Unique -}} if b.R.{{$txt.Function.ForeignName}} != nil { t.Error("failed to remove a from b's relationships") } - {{else -}} - if len(b.R.{{$txt.Function.ForeignName}}) != 0 { - t.Error("failed to remove a from b's relationships") - } - {{- end}} } {{end -}}{{/* end if foreign key nullable */}} {{- end -}}{{/* range */}}