Skip to content

Commit

Permalink
Test: Add test for "Wrong value when Find with JOIN and same column n…
Browse files Browse the repository at this point in the history
…ame" (#5711)
  • Loading branch information
StephanoGeorge committed Sep 27, 2022
1 parent 11592ad commit c6b2078
Showing 1 changed file with 30 additions and 0 deletions.
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.Index(results[0].Pet.Name, "_pet_") == -1 {
t.Fatalf("wrong pet name")
}
}

0 comments on commit c6b2078

Please sign in to comment.