@@ -182,6 +182,7 @@ func (s *Struct) UpdateForTag(table string, tag string, value interface{}) *Upda
182182 } else {
183183 val = dereferencedValue (val )
184184 }
185+
185186 data := val .Interface ()
186187 assignments = append (assignments , ub .Assign (quoted [i ], data ))
187188 }
@@ -252,23 +253,60 @@ func (s *Struct) buildColsAndValuesForTag(ib *InsertBuilder, tag string, value .
252253 if len (vs ) == 0 {
253254 return
254255 }
256+
255257 cols := make ([]string , 0 , len (fields ))
256258 values := make ([][]interface {}, len (vs ))
259+ nilCols := make ([]int , len (fields ))
257260
258- for _ , f := range fields {
261+ for idx , f := range fields {
259262 cols = append (cols , f )
260263 name := sf .fieldAlias [f ]
264+ shouldOmitempty := false
265+
266+ if omitEmptyTagMap , ok := sf .omitEmptyFields [f ]; ok {
267+ if omitEmptyTagMap .containsAny ("" , tag ) {
268+ shouldOmitempty = true
269+ }
270+ }
261271
262272 for i , v := range vs {
263- data := v .FieldByName (name ).Interface ()
264- values [i ] = append (values [i ], data )
273+ val := v .FieldByName (name )
274+
275+ if isEmptyValue (val ) && shouldOmitempty {
276+ nilCols [idx ]++
277+ }
278+
279+ val = dereferencedValue (val )
280+
281+ if val .IsValid () {
282+ values [i ] = append (values [i ], val .Interface ())
283+ } else {
284+ values [i ] = append (values [i ], nil )
285+ }
286+ }
287+ }
288+
289+ // Try to filter out nil values if possible.
290+ filteredCols := make ([]string , 0 , len (cols ))
291+ filteredValues := make ([][]interface {}, len (values ))
292+
293+ for i , cnt := range nilCols {
294+ // If all values are nil in a column, ignore the column completedly.
295+ if cnt == len (values ) {
296+ continue
297+ }
298+
299+ filteredCols = append (filteredCols , cols [i ])
300+
301+ for n , value := range values {
302+ filteredValues [n ] = append (filteredValues [n ], value [i ])
265303 }
266304 }
267305
268- cols = s .quoteFields (sf , cols )
269- ib .Cols (cols ... )
306+ filteredCols = s .quoteFields (sf , filteredCols )
307+ ib .Cols (filteredCols ... )
270308
271- for _ , value := range values {
309+ for _ , value := range filteredValues {
272310 ib .Values (value ... )
273311 }
274312}
0 commit comments