Skip to content

Issue when combining orderBy and populate #6757

@bhdouglass

Description

@bhdouglass

Describe the bug

When using an orderBy that includes a column in a relationship that is not part of the populate array, I'm getting an error like no such column: m1.name. The relationship in the orderBy is 3 levels deep (parent -> child -> grandchild) but the populate only includes the second level (parent -> child).

Example:

orm.em.find(
  Equipment, 
  {}, 
  { 
    populate: ['model'], 
    orderBy: { model: { manufacturer: { name: QueryOrder.ASC } } } 
  }
)

The table alias that is generated is the 2nd level relationship, not the 3rd level as expected (in addition to also including the correct alias). The generated sql from my repoduction repo looks like this:

select `e0`.*, `m1`.`id` as `m1__id`, `m1`.`model_name` as `m1__model_name`, `m1`.`manufacturer_id` as `m1__manufacturer_id` from `equipment` as `e0` left join `model` as `m1` on `e0`.`model_id` = `m1`.`id` left join `manufacturer` as `m2` on `m1`.`manufacturer_id` = `m2`.`id` order by `m2`.`name` asc, `m1`.`name` asc

Where I would expect it to look like this:

select `e0`.*, `m1`.`id` as `m1__id`, `m1`.`model_name` as `m1__model_name`, `m1`.`manufacturer_id` as `m1__manufacturer_id` from `equipment` as `e0` left join `model` as `m1` on `e0`.`model_id` = `m1`.`id` left join `manufacturer` as `m2` on `m1`.`manufacturer_id` = `m2`.`id` order by `m2`.`name` asc

Reproduction

https://github.com/bhdouglass/mikro-orm-reproduction

What driver are you using?

@mikro-orm/postgresql

MikroORM version

6.4.16

Node.js version

20.13

Operating system

Linux

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions