mirror of https://github.com/go-gorm/gorm.git
fix: nested joins alias (#6265)
This commit is contained in:
parent
1f763c81cb
commit
407bedae0a
|
@ -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{
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue