From 041cd3dd31a68a3e9ad20fcddb46085df5aebc76 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 13 Sep 2016 09:29:36 +0800 Subject: [PATCH] Fix scan ignored fields, close #1117 --- scope.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/scope.go b/scope.go index 23a5701b..c00cd0a1 100644 --- a/scope.go +++ b/scope.go @@ -454,10 +454,10 @@ func (scope *Scope) quoteIfPossible(str string) string { func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) { var ( ignored interface{} - selectFields []*Field values = make([]interface{}, len(columns)) + selectFields []*Field selectedColumnsMap = map[string]int{} - resetFields = map[*Field]int{} + resetFields = map[int]*Field{} ) 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.Elem().Set(field.Field.Addr()) values[index] = reflectValue.Interface() - resetFields[field] = index + resetFields[index] = field } selectedColumnsMap[column] = fieldIndex - break + + if field.IsNormal { + break + } } } } 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() { field.Field.Set(v) } @@ -861,7 +864,6 @@ func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[strin if field.IsNormal { hasUpdate = true if err == ErrUnaddressable { - fmt.Println(err) results[field.DBName] = value } else { results[field.DBName] = field.Field.Interface()