forked from mirror/gorm
fixed:panic when create value from nil struct pointer. (#4771)
* fixed:create nil pointer * fixed:panic when create value from nil struct pointer.
This commit is contained in:
parent
418c60c83c
commit
ec58e3319f
|
@ -77,7 +77,12 @@ func Parse(dest interface{}, cacheStore *sync.Map, namer Namer) (*Schema, error)
|
|||
return nil, fmt.Errorf("%w: %+v", ErrUnsupportedDataType, dest)
|
||||
}
|
||||
|
||||
modelType := reflect.Indirect(reflect.ValueOf(dest)).Type()
|
||||
value := reflect.ValueOf(dest)
|
||||
if value.Kind() == reflect.Ptr && value.IsNil() {
|
||||
value = reflect.New(value.Type().Elem())
|
||||
}
|
||||
modelType := reflect.Indirect(value).Type()
|
||||
|
||||
if modelType.Kind() == reflect.Interface {
|
||||
modelType = reflect.Indirect(reflect.ValueOf(dest)).Elem().Type()
|
||||
}
|
||||
|
|
|
@ -517,3 +517,12 @@ func TestCreateFromSubQuery(t *testing.T) {
|
|||
t.Errorf("invalid insert SQL, got %v", result.Statement.SQL.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateNilPointer(t *testing.T) {
|
||||
var user *User
|
||||
|
||||
err := DB.Create(user).Error
|
||||
if err == nil || err != gorm.ErrInvalidValue {
|
||||
t.Fatalf("it is not ErrInvalidValue")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue