-
-
Notifications
You must be signed in to change notification settings - Fork 502
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
Column missing in order clause if pagination is active and the column is the result of a subquery #4104
Comments
I don't think you can order by an alias, it would have to be an expression - so the subquery itself. |
… what is beyond my knowledge since it seems to be more complicated than |
Hmm maybe not, we have an example in the docs that is using subquery for ordering: const knex = em.getKnex();
const qb1 = em.createQueryBuilder(Book2, 'b').count('b.uuid', true).where({ author: knex.ref('a.id') }).as('Author2.booksTotal');
const qb2 = em.createQueryBuilder(Author2, 'a');
qb2.select(['*', qb1]).orderBy({ booksTotal: 'desc' });
console.log(qb2.getQuery());
// select `a`.*, (select count(distinct `b`.`uuid_pk`) as `count` from `book2` as `b` where `b`.`author_id` = `a`.`id`) as `books_total` from `author2` as `a` order by `books_total` desc https://mikro-orm.io/docs/query-builder#using-sub-queries The code is also part of the test suite. I just confirmed I can also execute the query (the tests are only asserting how it looks). Maybe try to define the virtual property for it and use the same approach, aliasing with the entity name (note the edit: the docs even explicitly mention |
True, this works (and I derived my code from this example). But does it also work with |
Right, now I see what's happening. I thought the problem is in the outer order by, but it's the inner one. Yeah that sounds like something fixable, I guess it should be omitted from the inner order by, as it has no effect on it? |
Not sure if the inner |
True, it needs to be where the limit is applied. |
Since I'm evaluating mikro orm for a project for just a day, It's hard for me to contribute with a pull request. Maybe you can point me to the right direction so I could start thinking of a fix? |
This is where the pagination happens: https://github.com/mikro-orm/mikro-orm/blob/master/packages/knex/src/query/QueryBuilder.ts#L1055 |
Describe the bug
First I have to admit: not sure if its a bug or if there is a workaround for the problem.
If I add a colum to a query with a subquery, the column will not be part of the subquery in the where clause which is automatically created if pagination is active (default). Therefore I cannot use this added column in the sort clause, because pagination adds this column also to the subquery in the where clause with
min(column)
Stack trace
To Reproduce
Steps to reproduce the behavior:
Expected behavior
I'd like to sort by the columnt
sortcontent
as well.Additional context
Add any other context about the problem here.
Versions
The text was updated successfully, but these errors were encountered: