Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Wrong value when Find with JOIN and same column name (#5711) #5723

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 11 additions & 19 deletions scan.go
Expand Up @@ -200,28 +200,20 @@ func Scan(rows Rows, db *DB, mode ScanMode) {

// Not Pluck
if sch != nil {
schFieldsCount := len(sch.Fields)
for idx, column := range columns {
if field := sch.LookUpField(column); field != nil && field.Readable {
if curIndex, ok := selectedColumnsMap[column]; ok {
fields[idx] = field // handle duplicate fields
offset := curIndex + 1
// handle sch inconsistent with database
// like Raw(`...`).Scan
if schFieldsCount > offset {
for fieldIndex, selectField := range sch.Fields[offset:] {
if selectField.DBName == column && selectField.Readable {
selectedColumnsMap[column] = curIndex + fieldIndex + 1
fields[idx] = selectField
break
}
}
for i, field := range sch.Fields[selectedColumnsMap[column]:] {
if field.DBName == column {
if field.Readable {
fields[idx] = field
selectedColumnsMap[column] += i + 1
}
} else {
fields[idx] = field
selectedColumnsMap[column] = idx
break
}
} else if names := strings.Split(column, "__"); len(names) > 1 {
}
if fields[idx] != nil {
continue
}
if names := strings.Split(column, "__"); len(names) > 1 {
if rel, ok := sch.Relationships.Relations[names[0]]; ok {
if field := rel.FieldSchema.LookUpField(strings.Join(names[1:], "__")); field != nil && field.Readable {
fields[idx] = field
Expand Down
30 changes: 30 additions & 0 deletions tests/joins_test.go
Expand Up @@ -3,6 +3,7 @@ package tests_test
import (
"regexp"
"sort"
"strings"
"testing"

"gorm.io/gorm"
Expand Down Expand Up @@ -229,3 +230,32 @@ func TestJoinWithSoftDeleted(t *testing.T) {
t.Fatalf("joins NamedPet and Account should not empty:%v", user2)
}
}

func TestJoinWithSameColumnName(t *testing.T) {
user := GetUser("TestJoinWithSameColumnName", Config{
Languages: 1,
Pets: 1,
})
DB.Create(user)
type UserSpeak struct {
UserID uint
LanguageCode string
}
type Result struct {
User
UserSpeak
Language
Pet
}
results := make([]Result, 0, 1)
DB.Select("*").Table("users").Joins("JOIN user_speaks ON user_speaks.user_id = users.id").
Joins("JOIN languages ON languages.code = user_speaks.language_code").
Joins("LEFT OUTER JOIN pets ON pets.user_id = users.id").Find(&results)
if len(results) == 0 {
t.Fatalf("no record find")
} else if results[0].Pet.UserID == nil {
t.Fatalf("wrong user id in pet")
} else if !strings.Contains(results[0].Pet.Name, "_pet_") {
t.Fatalf("wrong pet name")
}
}