mirror of https://github.com/go-gorm/gorm.git
Fix use pointer of Valuer as foreign key, close #3212
This commit is contained in:
parent
2cbdd29f26
commit
7c2ecdfc1c
|
@ -742,15 +742,16 @@ func (field *Field) setupValuerAndSetter() {
|
|||
} else if _, ok := fieldValue.Elem().Interface().(sql.Scanner); ok {
|
||||
// pointer scanner
|
||||
field.Set = func(value reflect.Value, v interface{}) (err error) {
|
||||
reflectV := reflect.ValueOf(v)
|
||||
|
||||
if valuer, ok := v.(driver.Valuer); ok {
|
||||
if valuer == nil {
|
||||
if valuer == nil || reflectV.IsNil() {
|
||||
field.ReflectValueOf(value).Set(reflect.New(field.FieldType).Elem())
|
||||
} else {
|
||||
v, _ = valuer.Value()
|
||||
}
|
||||
}
|
||||
|
||||
reflectV := reflect.ValueOf(v)
|
||||
if reflectV.Type().AssignableTo(field.FieldType) {
|
||||
field.ReflectValueOf(value).Set(reflectV)
|
||||
} else if reflectV.Kind() == reflect.Ptr {
|
||||
|
|
|
@ -136,6 +136,8 @@ type ScannerValuerStruct struct {
|
|||
Strings StringsSlice
|
||||
Structs StructsSlice
|
||||
Role Role
|
||||
UserID *sql.NullInt64
|
||||
User User
|
||||
}
|
||||
|
||||
type EncryptedData []byte
|
||||
|
|
Loading…
Reference in New Issue