forked from mirror/gorm
Fix scan columns with same name
This commit is contained in:
parent
846a2d401a
commit
7c43d9fc36
|
@ -544,7 +544,7 @@ func TestJoinsWithSelect(t *testing.T) {
|
|||
DB.Save(&user)
|
||||
|
||||
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" {
|
||||
t.Errorf("Should find all two emails with Join select")
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ type User struct {
|
|||
Age int64
|
||||
UserNum Num
|
||||
Name string `sql:"size:255"`
|
||||
Email string
|
||||
Birthday time.Time // Time
|
||||
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
|
||||
|
|
|
@ -31,9 +31,14 @@ func TestFirstAndLast(t *testing.T) {
|
|||
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")
|
||||
}
|
||||
|
||||
if user.Email != "" {
|
||||
t.Errorf("User's Email should be blank as no one set it")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFirstAndLastWithNoStdPrimaryKey(t *testing.T) {
|
||||
|
|
6
scope.go
6
scope.go
|
@ -462,10 +462,10 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
|
|||
|
||||
selectFields = fields
|
||||
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.Field.Kind() == reflect.Ptr {
|
||||
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()
|
||||
resetFields[field] = index
|
||||
}
|
||||
|
||||
selectedColumnsMap[column] = fieldIndex
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue