mirror of https://github.com/go-gorm/gorm.git
Fix get scanner type
This commit is contained in:
parent
f4e705b749
commit
2d866090e4
|
@ -45,7 +45,7 @@ type Relationship struct {
|
|||
|
||||
func (scope *Scope) GenerateSqlTag(field *StructField) {
|
||||
var sqlType string
|
||||
reflectValue := reflect.New(field.Struct.Type)
|
||||
reflectValue := reflect.Indirect(reflect.New(field.Struct.Type))
|
||||
|
||||
if value, ok := field.SqlSettings["TYPE"]; ok {
|
||||
sqlType = value
|
||||
|
@ -58,12 +58,13 @@ func (scope *Scope) GenerateSqlTag(field *StructField) {
|
|||
|
||||
if field.IsScanner {
|
||||
var getScannerValue func(reflect.Value)
|
||||
getScannerValue = func(reflectValue reflect.Value) {
|
||||
if _, isScanner := reflect.New(reflectValue.Type()).Interface().(sql.Scanner); isScanner {
|
||||
getScannerValue = func(value reflect.Value) {
|
||||
reflectValue = value
|
||||
if _, isScanner := reflect.New(reflectValue.Type()).Interface().(sql.Scanner); isScanner && reflectValue.Kind() == reflect.Struct {
|
||||
getScannerValue(reflectValue.Field(0))
|
||||
}
|
||||
}
|
||||
getScannerValue(reflectValue.Field(0))
|
||||
getScannerValue(reflectValue)
|
||||
}
|
||||
|
||||
if sqlType == "" {
|
||||
|
@ -188,7 +189,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
|
||||
if many2many != "" {
|
||||
kind = "many_to_many"
|
||||
} else if !reflect.New(typ).FieldByName(foreignKey).IsValid() {
|
||||
} else if !reflect.New(typ).Elem().FieldByName(foreignKey).IsValid() {
|
||||
foreignKey = ""
|
||||
}
|
||||
|
||||
|
@ -239,6 +240,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
}
|
||||
|
||||
for _, field := range modelStruct.StructFields {
|
||||
if field.IsNormal {
|
||||
if modelStruct.PrimaryKeyField == nil && field.DBName == "id" {
|
||||
field.IsPrimaryKey = true
|
||||
modelStruct.PrimaryKeyField = field
|
||||
|
@ -246,6 +248,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
|
|||
|
||||
scope.GenerateSqlTag(field)
|
||||
}
|
||||
}
|
||||
|
||||
return &modelStruct
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue