diff --git a/orm/gen/templates.go b/orm/gen/templates.go index e965b68f..060fc3a8 100644 --- a/orm/gen/templates.go +++ b/orm/gen/templates.go @@ -287,7 +287,6 @@ func columnSetter(i Importer, tables []drivers.Table, table, column, to string, } col := t.GetColumn(column) - optional = optional && col.Default != "" // without default, it is non-optional switch { case optional && col.Nullable: i.Import("github.com/aarondl/opt/omitnull") @@ -468,7 +467,7 @@ func setDeps(i Importer, tables []drivers.Table, aliases Aliases, r orm.Relation "%s.%s", extObjVarName, malias.Columns[mapp.ExternalColumn], - ), optional && kside.TableName == foreign) + ), shouldCreate || (optional && kside.TableName == foreign)) mret = append(mret, fmt.Sprintf(`%s.%s = %s`, objVarName, @@ -505,7 +504,7 @@ func relatedUpdateValues(i Importer, tables []drivers.Table, aliases Aliases, r "%s.%s", extObjVarName, malias.Columns[mapp.ExternalColumn], - ), false) + ), true) mret = append(mret, fmt.Sprintf("%s: %s,", oalias.Columns[mapp.Column], diff --git a/orm/gen/templates/01_types.go.tpl b/orm/gen/templates/01_types.go.tpl index edafc683..034feb5b 100644 --- a/orm/gen/templates/01_types.go.tpl +++ b/orm/gen/templates/01_types.go.tpl @@ -32,24 +32,20 @@ type {{$tAlias.UpSingular}} struct { } {{if .Table.PKey -}} -// Optional{{$tAlias.UpSingular}} is used for insert/upsert operations -// Fields that have default values are optional, and do not have to be set -// Fields without default values must be set or the zero value will be used +// Optional{{$tAlias.UpSingular}} is used for insert/upsert/update operations +// All values are optional, and do not have to be set // Generated columns are not included type Optional{{$tAlias.UpSingular}} struct { {{- range $column := .Table.Columns -}} {{- if $column.Generated}}{{continue}}{{end -}} {{- $colAlias := $tAlias.Column $column.Name -}} - {{- $colTyp := $column.Type -}} - {{- if and $column.Default $column.Nullable -}} + {{- $colTyp := "" -}} + {{- if $column.Nullable -}} {{- $.Importer.Import "github.com/aarondl/opt/omitnull" -}} {{- $colTyp = printf "omitnull.Val[%s]" $column.Type -}} - {{- else if $column.Default -}} + {{- else -}} {{- $.Importer.Import "github.com/aarondl/opt/omit" -}} {{- $colTyp = printf "omit.Val[%s]" $column.Type -}} - {{- else if $column.Nullable -}} - {{- $.Importer.Import "github.com/aarondl/opt/null" -}} - {{- $colTyp = printf "null.Val[%s]" $column.Type -}} {{- end -}} {{$colAlias}} {{$colTyp}} `db:"{{dbTag $table $column}}"` {{end -}}