diff --git a/templates/12_insert.tpl b/templates/12_insert.tpl index a047770..35f0ef8 100644 --- a/templates/12_insert.tpl +++ b/templates/12_insert.tpl @@ -100,20 +100,23 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string if err != nil { return ErrSyncFail } - - if lastID != 0 { - {{- $colName := index .Table.PKey.Columns 0 -}} - {{- $col := .Table.GetColumn $colName -}} - {{- $colTitled := $colName | singular | titleCase}} - o.{{$colTitled}} = {{$col.Type}}(lastID) - identifierCols = []interface{}{lastID} - } else { - identifierCols = []interface{}{ - {{range .Table.PKey.Columns -}} - o.{{. | singular | titleCase}}, - {{end -}} - } + + {{- $colName := index .Table.PKey.Columns 0 -}} + {{- $col := .Table.GetColumn $colName -}} + {{- $colTitled := $colName | singular | titleCase}} + {{if eq 1 (len .Table.PKey.Columns)}} + {{$cnames := .Table.Columns | filterColumnsByDefault true | columnNames}} + {{if setInclude $colName $cnames}} + o.{{$colTitled}} = {{$col.Type}}(lastID) + identifierCols = []interface{}{lastID} + {{end}} + {{else}} + identifierCols = []interface{}{ + {{range .Table.PKey.Columns -}} + o.{{. | singular | titleCase}}, + {{end -}} } + {{end}} if lastID == 0 || len(cache.retMapping) != 1 || cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] { if boil.DebugMode { diff --git a/templates/14_upsert.tpl b/templates/14_upsert.tpl index 18f955f..6710f13 100644 --- a/templates/14_upsert.tpl +++ b/templates/14_upsert.tpl @@ -142,19 +142,22 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, {{if ne .DriverName return ErrSyncFail } - if lastID != 0 { - {{- $colName := index .Table.PKey.Columns 0 -}} - {{- $col := .Table.GetColumn $colName -}} - {{- $colTitled := $colName | singular | titleCase}} - o.{{$colTitled}} = {{$col.Type}}(lastID) - identifierCols = []interface{}{lastID} - } else { - identifierCols = []interface{}{ - {{range .Table.PKey.Columns -}} - o.{{. | singular | titleCase}}, - {{end -}} - } + {{- $colName := index .Table.PKey.Columns 0 -}} + {{- $col := .Table.GetColumn $colName -}} + {{- $colTitled := $colName | singular | titleCase}} + {{if eq 1 (len .Table.PKey.Columns)}} + {{$cnames := .Table.Columns | filterColumnsByDefault true | columnNames}} + {{if setInclude $colName $cnames}} + o.{{$colTitled}} = {{$col.Type}}(lastID) + identifierCols = []interface{}{lastID} + {{end}} + {{else}} + identifierCols = []interface{}{ + {{range .Table.PKey.Columns -}} + o.{{. | singular | titleCase}}, + {{end -}} } + {{end}} if lastID == 0 || len(cache.retMapping) != 1 || cache.retMapping[0] == {{$varNameSingular}}Mapping["{{$colTitled}}"] { if boil.DebugMode { diff --git a/templates_test/relationship_to_one_setops.tpl b/templates_test/relationship_to_one_setops.tpl index c082f58..b59313e 100644 --- a/templates_test/relationship_to_one_setops.tpl +++ b/templates_test/relationship_to_one_setops.tpl @@ -2,7 +2,7 @@ {{- $dot := .Dot -}} {{- with .Rel -}} {{- $varNameSingular := .ForeignKey.Table | singular | camelCase -}} -{{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase}} +{{- $foreignVarNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}} {{- $foreignTable := getTable $dot.Tables .ForeignKey.ForeignTable -}} {{- $foreignTableFKeyCol := $foreignTable.GetColumn .ForeignKey.ForeignColumn -}} {{- $usesBytes := eq "[]byte" $foreignTableFKeyCol.Type -}}