Fix scan columns with same name

This commit is contained in:
Jinzhu 2016-03-10 17:35:19 +08:00
parent 846a2d401a
commit 7c43d9fc36
4 changed files with 13 additions and 5 deletions

View File

@ -544,7 +544,7 @@ func TestJoinsWithSelect(t *testing.T) {
DB.Save(&user) DB.Save(&user)
var results []result var results []result
DB.Table("users").Select("name, email").Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins_with_select").Scan(&results) DB.Table("users").Select("name, emails.email").Joins("left join emails on emails.user_id = users.id").Where("name = ?", "joins_with_select").Scan(&results)
if len(results) != 2 || results[0].Email != "join1@example.com" || results[1].Email != "join2@example.com" { if len(results) != 2 || results[0].Email != "join1@example.com" || results[1].Email != "join2@example.com" {
t.Errorf("Should find all two emails with Join select") t.Errorf("Should find all two emails with Join select")
} }

View File

@ -17,6 +17,7 @@ type User struct {
Age int64 Age int64
UserNum Num UserNum Num
Name string `sql:"size:255"` Name string `sql:"size:255"`
Email string
Birthday time.Time // Time Birthday time.Time // Time
CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically
UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically

View File

@ -31,9 +31,14 @@ func TestFirstAndLast(t *testing.T) {
t.Errorf("Find first record as slice") t.Errorf("Find first record as slice")
} }
if DB.Joins("left join emails on emails.user_id = users.id").First(&User{}).Error != nil { var user User
if DB.Joins("left join emails on emails.user_id = users.id").First(&user).Error != nil {
t.Errorf("Should not raise any error when order with Join table") t.Errorf("Should not raise any error when order with Join table")
} }
if user.Email != "" {
t.Errorf("User's Email should be blank as no one set it")
}
} }
func TestFirstAndLastWithNoStdPrimaryKey(t *testing.T) { func TestFirstAndLastWithNoStdPrimaryKey(t *testing.T) {

View File

@ -462,10 +462,10 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
selectFields = fields selectFields = fields
if idx, ok := selectedColumnsMap[column]; ok { if idx, ok := selectedColumnsMap[column]; ok {
selectFields = selectFields[idx:] selectFields = selectFields[idx+1:]
} }
for _, field := range selectFields { for fieldIndex, field := range selectFields {
if field.DBName == column { if field.DBName == column {
if field.Field.Kind() == reflect.Ptr { if field.Field.Kind() == reflect.Ptr {
values[index] = field.Field.Addr().Interface() values[index] = field.Field.Addr().Interface()
@ -475,6 +475,8 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
values[index] = reflectValue.Interface() values[index] = reflectValue.Interface()
resetFields[field] = index resetFields[field] = index
} }
selectedColumnsMap[column] = fieldIndex
break break
} }
} }