mirror of https://github.com/go-gorm/gorm.git
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)
|
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")
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,8 @@ type User struct {
|
||||||
Id int64
|
Id int64
|
||||||
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
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
6
scope.go
6
scope.go
|
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue