From b27095e8a1994f48f9099242d191acd43542e458 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 13 Oct 2021 21:01:32 +0800 Subject: [PATCH] Refactor Convert SQL null values to zero values for model fields which are not pointers #4710 --- callbacks/create.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/callbacks/create.go b/callbacks/create.go index ebfc8426..c889caf6 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -149,8 +149,11 @@ func CreateWithReturning(db *gorm.DB) { switch db.Statement.ReflectValue.Kind() { case reflect.Slice, reflect.Array: - c := db.Statement.Clauses["ON CONFLICT"] - onConflict, _ := c.Expression.(clause.OnConflict) + var ( + c = db.Statement.Clauses["ON CONFLICT"] + onConflict, _ = c.Expression.(clause.OnConflict) + resetFieldValues = map[int]reflect.Value{} + ) for rows.Next() { BEGIN: @@ -159,7 +162,6 @@ func CreateWithReturning(db *gorm.DB) { break } - resetFields := map[int]*schema.Field{} for idx, field := range fields { fieldValue := field.ReflectValueOf(reflectValue) @@ -179,7 +181,7 @@ func CreateWithReturning(db *gorm.DB) { reflectValue := reflect.New(reflect.PtrTo(field.FieldType)) reflectValue.Elem().Set(fieldValue.Addr()) values[idx] = reflectValue.Interface() - resetFields[idx] = field + resetFieldValues[idx] = fieldValue } } @@ -188,30 +190,31 @@ func CreateWithReturning(db *gorm.DB) { db.AddError(err) } - for idx, field := range resetFields { - if v := reflect.ValueOf(values[idx]).Elem().Elem(); v.IsValid() { - field.ReflectValueOf(reflectValue).Set(v) + for idx, fv := range resetFieldValues { + if v := reflect.ValueOf(values[idx]).Elem(); !v.IsNil() { + fv.Set(v.Elem()) } } } case reflect.Struct: - resetFields := map[int]*schema.Field{} + resetFieldValues := map[int]reflect.Value{} for idx, field := range fields { if field.FieldType.Kind() == reflect.Ptr { values[idx] = field.ReflectValueOf(db.Statement.ReflectValue).Addr().Interface() } else { reflectValue := reflect.New(reflect.PtrTo(field.FieldType)) - reflectValue.Elem().Set(field.ReflectValueOf(db.Statement.ReflectValue).Addr()) + fieldValue := field.ReflectValueOf(db.Statement.ReflectValue) + reflectValue.Elem().Set(fieldValue.Addr()) values[idx] = reflectValue.Interface() - resetFields[idx] = field + resetFieldValues[idx] = fieldValue } } if rows.Next() { db.RowsAffected++ db.AddError(rows.Scan(values...)) - for idx, field := range resetFields { - if v := reflect.ValueOf(values[idx]).Elem().Elem(); v.IsValid() { - field.ReflectValueOf(db.Statement.ReflectValue).Set(v) + for idx, fv := range resetFieldValues { + if v := reflect.ValueOf(values[idx]).Elem(); !v.IsNil() { + fv.Set(v.Elem()) } } }