From cb591a71299532f881a104cdb0abf7ae5b794a6f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 23 Oct 2020 18:40:05 +0800 Subject: [PATCH] Fix panic when using FirstOrCreate with soft delete, close #3671 --- schema/field.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/schema/field.go b/schema/field.go index e7f5b708..b303fb30 100644 --- a/schema/field.go +++ b/schema/field.go @@ -762,13 +762,15 @@ func (field *Field) setupValuerAndSetter() { // pointer scanner field.Set = func(value reflect.Value, v interface{}) (err error) { 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) } else if reflectV.Kind() == reflect.Ptr { - if reflectV.IsNil() { + if reflectV.IsNil() || !reflectV.IsValid() { field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem()) } else { - err = field.Set(value, reflectV.Elem().Interface()) + return field.Set(value, reflectV.Elem().Interface()) } } else { fieldValue := field.ReflectValueOf(value)