fix: nested joins alias (#6265)

This commit is contained in:
Cr 2023-04-26 22:19:32 +08:00 committed by GitHub
parent 1f763c81cb
commit 407bedae0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -234,7 +234,12 @@ func BuildQuerySQL(db *gorm.DB) {
fromClause.Joins = append(fromClause.Joins, genJoinClause(join.JoinType, parentTableName, rel))
specifiedRelationsName[nestedAlias] = nil
}
parentTableName = rel.Name
if parentTableName != clause.CurrentTable {
parentTableName = utils.NestedRelationName(parentTableName, rel.Name)
} else {
parentTableName = rel.Name
}
}
} else {
fromClause.Joins = append(fromClause.Joins, clause.Join{

View File

@ -329,8 +329,19 @@ func TestJoinArgsWithDB(t *testing.T) {
func TestNestedJoins(t *testing.T) {
users := []User{
{
Name: "nested-joins-1",
Manager: GetUser("nested-joins-manager-1", Config{Company: true, NamedPet: true}),
Name: "nested-joins-1",
Manager: &User{
Name: "nested-joins-manager-1",
Company: Company{
Name: "nested-joins-manager-company-1",
},
NamedPet: &Pet{
Name: "nested-joins-manager-namepet-1",
Toy: Toy{
Name: "nested-joins-manager-namepet-toy-1",
},
},
},
NamedPet: &Pet{Name: "nested-joins-namepet-1", Toy: Toy{Name: "nested-joins-namepet-toy-1"}},
},
{
@ -352,6 +363,7 @@ func TestNestedJoins(t *testing.T) {
Joins("Manager").
Joins("Manager.Company").
Joins("Manager.NamedPet").
Joins("Manager.NamedPet.Toy").
Joins("NamedPet").
Joins("NamedPet.Toy").
Find(&users2, "users.id IN ?", userIDs).Error; err != nil {