forked from mirror/gorm
fix: reset null value in slice (#5417)
* fix: reset null value in slice * fix: can not set field in-place in join
This commit is contained in:
parent
d01de7232b
commit
8d45714628
17
scan.go
17
scan.go
|
@ -66,18 +66,23 @@ func (db *DB) scanIntoStruct(rows Rows, reflectValue reflect.Value, values []int
|
|||
db.RowsAffected++
|
||||
db.AddError(rows.Scan(values...))
|
||||
|
||||
joinedSchemaMap := make(map[*schema.Field]interface{}, 0)
|
||||
for idx, field := range fields {
|
||||
if field != nil {
|
||||
if len(joinFields) == 0 || joinFields[idx][0] == nil {
|
||||
db.AddError(field.Set(db.Statement.Context, reflectValue, values[idx]))
|
||||
} else {
|
||||
relValue := joinFields[idx][0].ReflectValueOf(db.Statement.Context, reflectValue)
|
||||
if relValue.Kind() == reflect.Ptr && relValue.IsNil() {
|
||||
if value := reflect.ValueOf(values[idx]).Elem(); value.Kind() == reflect.Ptr && value.IsNil() {
|
||||
continue
|
||||
}
|
||||
joinSchema := joinFields[idx][0]
|
||||
relValue := joinSchema.ReflectValueOf(db.Statement.Context, reflectValue)
|
||||
if relValue.Kind() == reflect.Ptr {
|
||||
if _, ok := joinedSchemaMap[joinSchema]; !ok {
|
||||
if value := reflect.ValueOf(values[idx]).Elem(); value.Kind() == reflect.Ptr && value.IsNil() {
|
||||
continue
|
||||
}
|
||||
|
||||
relValue.Set(reflect.New(relValue.Type().Elem()))
|
||||
relValue.Set(reflect.New(relValue.Type().Elem()))
|
||||
joinedSchemaMap[joinSchema] = nil
|
||||
}
|
||||
}
|
||||
db.AddError(joinFields[idx][1].Set(db.Statement.Context, relValue, values[idx]))
|
||||
}
|
||||
|
|
|
@ -587,6 +587,8 @@ func (field *Field) setupValuerAndSetter() {
|
|||
case **bool:
|
||||
if data != nil && *data != nil {
|
||||
field.ReflectValueOf(ctx, value).SetBool(**data)
|
||||
} else {
|
||||
field.ReflectValueOf(ctx, value).SetBool(false)
|
||||
}
|
||||
case bool:
|
||||
field.ReflectValueOf(ctx, value).SetBool(data)
|
||||
|
@ -606,6 +608,8 @@ func (field *Field) setupValuerAndSetter() {
|
|||
case **int64:
|
||||
if data != nil && *data != nil {
|
||||
field.ReflectValueOf(ctx, value).SetInt(**data)
|
||||
} else {
|
||||
field.ReflectValueOf(ctx, value).SetInt(0)
|
||||
}
|
||||
case int64:
|
||||
field.ReflectValueOf(ctx, value).SetInt(data)
|
||||
|
@ -670,6 +674,8 @@ func (field *Field) setupValuerAndSetter() {
|
|||
case **uint64:
|
||||
if data != nil && *data != nil {
|
||||
field.ReflectValueOf(ctx, value).SetUint(**data)
|
||||
} else {
|
||||
field.ReflectValueOf(ctx, value).SetUint(0)
|
||||
}
|
||||
case uint64:
|
||||
field.ReflectValueOf(ctx, value).SetUint(data)
|
||||
|
@ -722,6 +728,8 @@ func (field *Field) setupValuerAndSetter() {
|
|||
case **float64:
|
||||
if data != nil && *data != nil {
|
||||
field.ReflectValueOf(ctx, value).SetFloat(**data)
|
||||
} else {
|
||||
field.ReflectValueOf(ctx, value).SetFloat(0)
|
||||
}
|
||||
case float64:
|
||||
field.ReflectValueOf(ctx, value).SetFloat(data)
|
||||
|
@ -766,6 +774,8 @@ func (field *Field) setupValuerAndSetter() {
|
|||
case **string:
|
||||
if data != nil && *data != nil {
|
||||
field.ReflectValueOf(ctx, value).SetString(**data)
|
||||
} else {
|
||||
field.ReflectValueOf(ctx, value).SetString("")
|
||||
}
|
||||
case string:
|
||||
field.ReflectValueOf(ctx, value).SetString(data)
|
||||
|
|
|
@ -1258,3 +1258,80 @@ func TestQueryScannerWithSingleColumn(t *testing.T) {
|
|||
|
||||
AssertEqual(t, result2.data, 20)
|
||||
}
|
||||
|
||||
func TestQueryResetNullValue(t *testing.T) {
|
||||
type QueryResetItem struct {
|
||||
ID string `gorm:"type:varchar(5)"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type QueryResetNullValue struct {
|
||||
ID int
|
||||
Name string `gorm:"default:NULL"`
|
||||
Flag bool `gorm:"default:NULL"`
|
||||
Number1 int64 `gorm:"default:NULL"`
|
||||
Number2 uint64 `gorm:"default:NULL"`
|
||||
Number3 float64 `gorm:"default:NULL"`
|
||||
Now *time.Time `gorm:"defalut:NULL"`
|
||||
Item1Id string
|
||||
Item1 *QueryResetItem `gorm:"references:ID"`
|
||||
Item2Id string
|
||||
Item2 *QueryResetItem `gorm:"references:ID"`
|
||||
}
|
||||
|
||||
DB.Migrator().DropTable(&QueryResetNullValue{}, &QueryResetItem{})
|
||||
DB.AutoMigrate(&QueryResetNullValue{}, &QueryResetItem{})
|
||||
|
||||
now := time.Now()
|
||||
q1 := QueryResetNullValue{
|
||||
Name: "name",
|
||||
Flag: true,
|
||||
Number1: 100,
|
||||
Number2: 200,
|
||||
Number3: 300.1,
|
||||
Now: &now,
|
||||
Item1: &QueryResetItem{
|
||||
ID: "u_1_1",
|
||||
Name: "item_1_1",
|
||||
},
|
||||
Item2: &QueryResetItem{
|
||||
ID: "u_1_2",
|
||||
Name: "item_1_2",
|
||||
},
|
||||
}
|
||||
|
||||
q2 := QueryResetNullValue{
|
||||
Item1: &QueryResetItem{
|
||||
ID: "u_2_1",
|
||||
Name: "item_2_1",
|
||||
},
|
||||
Item2: &QueryResetItem{
|
||||
ID: "u_2_2",
|
||||
Name: "item_2_2",
|
||||
},
|
||||
}
|
||||
|
||||
var err error
|
||||
err = DB.Create(&q1).Error
|
||||
if err != nil {
|
||||
t.Errorf("failed to create:%v", err)
|
||||
}
|
||||
|
||||
err = DB.Create(&q2).Error
|
||||
if err != nil {
|
||||
t.Errorf("failed to create:%v", err)
|
||||
}
|
||||
|
||||
var qs []QueryResetNullValue
|
||||
err = DB.Joins("Item1").Joins("Item2").Find(&qs).Error
|
||||
if err != nil {
|
||||
t.Errorf("failed to find:%v", err)
|
||||
}
|
||||
|
||||
if len(qs) != 2 {
|
||||
t.Fatalf("find count not equal:%d", len(qs))
|
||||
}
|
||||
|
||||
AssertEqual(t, q1, qs[0])
|
||||
AssertEqual(t, q2, qs[1])
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue