mirror of https://github.com/go-gorm/gorm.git
Fix scan ignored fields, close #1117
This commit is contained in:
parent
3425c1d38d
commit
041cd3dd31
12
scope.go
12
scope.go
|
@ -454,10 +454,10 @@ func (scope *Scope) quoteIfPossible(str string) string {
|
||||||
func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
|
func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
|
||||||
var (
|
var (
|
||||||
ignored interface{}
|
ignored interface{}
|
||||||
selectFields []*Field
|
|
||||||
values = make([]interface{}, len(columns))
|
values = make([]interface{}, len(columns))
|
||||||
|
selectFields []*Field
|
||||||
selectedColumnsMap = map[string]int{}
|
selectedColumnsMap = map[string]int{}
|
||||||
resetFields = map[*Field]int{}
|
resetFields = map[int]*Field{}
|
||||||
)
|
)
|
||||||
|
|
||||||
for index, column := range columns {
|
for index, column := range columns {
|
||||||
|
@ -476,18 +476,21 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
|
||||||
reflectValue := reflect.New(reflect.PtrTo(field.Struct.Type))
|
reflectValue := reflect.New(reflect.PtrTo(field.Struct.Type))
|
||||||
reflectValue.Elem().Set(field.Field.Addr())
|
reflectValue.Elem().Set(field.Field.Addr())
|
||||||
values[index] = reflectValue.Interface()
|
values[index] = reflectValue.Interface()
|
||||||
resetFields[field] = index
|
resetFields[index] = field
|
||||||
}
|
}
|
||||||
|
|
||||||
selectedColumnsMap[column] = fieldIndex
|
selectedColumnsMap[column] = fieldIndex
|
||||||
|
|
||||||
|
if field.IsNormal {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
scope.Err(rows.Scan(values...))
|
scope.Err(rows.Scan(values...))
|
||||||
|
|
||||||
for field, index := range resetFields {
|
for index, field := range resetFields {
|
||||||
if v := reflect.ValueOf(values[index]).Elem().Elem(); v.IsValid() {
|
if v := reflect.ValueOf(values[index]).Elem().Elem(); v.IsValid() {
|
||||||
field.Field.Set(v)
|
field.Field.Set(v)
|
||||||
}
|
}
|
||||||
|
@ -861,7 +864,6 @@ func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[strin
|
||||||
if field.IsNormal {
|
if field.IsNormal {
|
||||||
hasUpdate = true
|
hasUpdate = true
|
||||||
if err == ErrUnaddressable {
|
if err == ErrUnaddressable {
|
||||||
fmt.Println(err)
|
|
||||||
results[field.DBName] = value
|
results[field.DBName] = value
|
||||||
} else {
|
} else {
|
||||||
results[field.DBName] = field.Field.Interface()
|
results[field.DBName] = field.Field.Interface()
|
||||||
|
|
Loading…
Reference in New Issue