diff --git a/templates/relationship_to_many_setops.tpl b/templates/relationship_to_many_setops.tpl index cdfb177..8a127a7 100644 --- a/templates/relationship_to_many_setops.tpl +++ b/templates/relationship_to_many_setops.tpl @@ -7,15 +7,16 @@ {{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}} {{- template "relationship_to_one_setops_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}} {{- else -}} - {{- $rel := textsFromRelationship $dot.Tables $table . -}} + {{- $rel := textsFromRelationship $dot.Tables $table .}} + // Add{{$rel.Function.Name}} adds the given related objects to the existing relationships // of the {{$table.Name | singular}}, optionally inserting them as new records. // Appends related to R.{{$rel.Function.Name}}. func (r *{{$rel.LocalTable.NameGo}}Loaded) Add{{$rel.Function.Name}}(exec boil.Executor, insert bool, related ...*{{$rel.ForeignTable.NameGo}}) error { return nil } +{{- if .ForeignColumnNullable}} -{{if .ForeignColumnNullable -}} // Set{{$rel.Function.Name}} removes all previously related items of the // {{$table.Name | singular}} replacing them completely with the passed // in related items, optionally inserting them as new records. @@ -30,7 +31,6 @@ func (r *{{$rel.LocalTable.NameGo}}Loaded) Remove{{$rel.Function.Name}}(exec boi return nil } {{end -}} - -{{end -}}{{- /* if unique foreign key */ -}} +{{- end -}}{{- /* if unique foreign key */ -}} {{- end -}}{{- /* range relationships */ -}} {{- end -}}{{- /* outer if join table */ -}} diff --git a/templates/relationship_to_one_setops.tpl b/templates/relationship_to_one_setops.tpl index e0da4ae..21b54b1 100644 --- a/templates/relationship_to_one_setops.tpl +++ b/templates/relationship_to_one_setops.tpl @@ -1,20 +1,26 @@ {{- define "relationship_to_one_setops_helper" -}} -{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}} +{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase}} + // Set{{.Function.Name}} of the {{.ForeignKey.Table | singular}} to the related item. // Sets R.{{.Function.Name}} to related. func (r *{{.LocalTable.NameGo}}Loaded) Set{{.Function.Name}}(exec boil.Executor, insert bool, related *{{.ForeignTable.NameGo}}) error { - return nil -} + r.{{.Function.Name}} = related + r.{{.Function.Name}}.{{.Function.ForeignAssignment}} = 5 + if insert { + return related.Insert() + } + + return exec.Exec(`update "{{.ForeignKey.Table}}" set "{{.ForeignKey.Column}}" = $1`, 5) +} +{{- if .ForeignKey.Nullable}} -{{if .ForeignKey.Nullable -}} // Remove{{.Function.Name}} relationship. // Sets R.{{.Function.Name}} to nil. func (r *{{.LocalTable.NameGo}}Loaded) Remove{{.Function.Name}}(exec boil.Executor) error { return nil } {{end -}} - -{{end -}} +{{- end -}} {{- if .Table.IsJoinTable -}} {{- else -}} {{- $dot := . -}} diff --git a/templates_test/relationship_to_many_setops.tpl b/templates_test/relationship_to_many_setops.tpl index e69de29..05afbef 100644 --- a/templates_test/relationship_to_many_setops.tpl +++ b/templates_test/relationship_to_many_setops.tpl @@ -0,0 +1,24 @@ +{{- if .Table.IsJoinTable -}} +{{- else -}} + {{- $dot := . -}} + {{- $table := .Table -}} + {{- range .Table.ToManyRelationships -}} + {{- $varNameSingular := .ForeignTable | singular | camelCase -}} + {{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}} +{{- template "relationship_to_one_setops_test_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}} + {{- else -}} + {{- $rel := textsFromRelationship $dot.Tables $table .}} + +func test{{$rel.LocalTable.NameGo}}ToManyAddOp{{$rel.Function.Name}}(t *testing.T) { +} +{{if .ForeignColumnNullable}} + +func test{{$rel.LocalTable.NameGo}}ToManySetOp{{$rel.Function.Name}}(t *testing.T) { +} + +func test{{$rel.LocalTable.NameGo}}ToManyRemoveOp{{$rel.Function.Name}}(t *testing.T) { +} +{{end -}} +{{- end -}}{{- /* if unique foreign key */ -}} +{{- end -}}{{- /* range relationships */ -}} +{{- end -}}{{- /* outer if join table */ -}} diff --git a/templates_test/relationship_to_one_setops.tpl b/templates_test/relationship_to_one_setops.tpl index e69de29..aff1d1c 100644 --- a/templates_test/relationship_to_one_setops.tpl +++ b/templates_test/relationship_to_one_setops.tpl @@ -0,0 +1,19 @@ +{{- define "relationship_to_one_setops_test_helper" -}} +{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}} +func test{{.LocalTable.NameGo}}ToOneSetOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) { +} +{{- if .ForeignKey.Nullable}} + +func test{{.LocalTable.NameGo}}ToOneRemoveOp{{.ForeignTable.NameGo}}_{{.Function.Name}}(t *testing.T) { +} +{{end -}} +{{- end -}} +{{- if .Table.IsJoinTable -}} +{{- else -}} + {{- $dot := . -}} + {{- range .Table.FKeys -}} + {{- $rel := textsFromForeignKey $dot.PkgName $dot.Tables $dot.Table .}} + +{{template "relationship_to_one_setops_test_helper" $rel -}} +{{- end -}} +{{- end -}}