Skip to content

Commit

Permalink
Make all fields in OptionalModel optional
Browse files Browse the repository at this point in the history
This is to make UpdateAll work properly
since we need to know all the fields that
are actually being set
  • Loading branch information
stephenafamo committed Nov 25, 2022
1 parent a15998b commit 954ab46
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
5 changes: 2 additions & 3 deletions orm/gen/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand Down
14 changes: 5 additions & 9 deletions orm/gen/templates/01_types.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down

0 comments on commit 954ab46

Please sign in to comment.