Fix use pointer of Valuer as foreign key, close #3212

This commit is contained in:
Jinzhu 2020-07-30 10:23:35 +08:00
parent 2cbdd29f26
commit 7c2ecdfc1c
2 changed files with 5 additions and 2 deletions

View File

@ -742,15 +742,16 @@ func (field *Field) setupValuerAndSetter() {
} else if _, ok := fieldValue.Elem().Interface().(sql.Scanner); ok { } else if _, ok := fieldValue.Elem().Interface().(sql.Scanner); ok {
// 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)
if valuer, ok := v.(driver.Valuer); ok { if valuer, ok := v.(driver.Valuer); ok {
if valuer == nil { if valuer == nil || reflectV.IsNil() {
field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem()) field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem())
} else { } else {
v, _ = valuer.Value() v, _ = valuer.Value()
} }
} }
reflectV := reflect.ValueOf(v)
if reflectV.Type().AssignableTo(field.FieldType) { 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 {

View File

@ -136,6 +136,8 @@ type ScannerValuerStruct struct {
Strings StringsSlice Strings StringsSlice
Structs StructsSlice Structs StructsSlice
Role Role Role Role
UserID *sql.NullInt64
User User
} }
type EncryptedData []byte type EncryptedData []byte