Fix panic when using FirstOrCreate with soft delete, close #3671

This commit is contained in:
Jinzhu 2020-10-23 18:40:05 +08:00
parent dd92f8bdc0
commit cb591a7129
1 changed files with 5 additions and 3 deletions

View File

@ -762,13 +762,15 @@ func (field *Field) setupValuerAndSetter() {
// pointer scanner // pointer scanner
field.Set = func(value reflect.Value, v interface{}) (err error) { field.Set = func(value reflect.Value, v interface{}) (err error) {
reflectV := reflect.ValueOf(v) reflectV := reflect.ValueOf(v)
if reflectV.Type().AssignableTo(field.FieldType) { if !reflectV.IsValid() {
field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem())
} else if reflectV.Type().AssignableTo(field.FieldType) {
field.ReflectValueOf(value).Set(reflectV) field.ReflectValueOf(value).Set(reflectV)
} else if reflectV.Kind() == reflect.Ptr { } else if reflectV.Kind() == reflect.Ptr {
if reflectV.IsNil() { if reflectV.IsNil() || !reflectV.IsValid() {
field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem()) field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem())
} else { } else {
err = field.Set(value, reflectV.Elem().Interface()) return field.Set(value, reflectV.Elem().Interface())
} }
} else { } else {
fieldValue := field.ReflectValueOf(value) fieldValue := field.ReflectValueOf(value)