Skip to content

Commit

Permalink
fix(knex): order by with a formula field should not include as fo…
Browse files Browse the repository at this point in the history
…r sub-queries (#2929)
  • Loading branch information
tsangste committed Mar 19, 2022
1 parent 19ab6e0 commit 74751fb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
const prop = this.helper.getProperty(f, a);
const type = this.platform.castColumn(prop);
orderBy.push({
[`min(${this.ref(this.helper.mapper(field, this.type))}${type})`]: direction,
[`min(${this.ref(this.helper.mapper(field, this.type, undefined, null))}${type})`]: direction,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/knex/src/query/QueryBuilderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ export class QueryBuilderHelper {

Utils.splitPrimaryKeys(field).forEach(f => {
const prop = this.getProperty(f, alias);
const noPrefix = (prop && prop.persist === false) || QueryBuilderHelper.isCustomExpression(f);
const noPrefix = (prop && prop.persist === false && !prop.formula) || QueryBuilderHelper.isCustomExpression(f);
const column = this.mapper(noPrefix ? f : `${alias}.${f}`, type, undefined, null);
/* istanbul ignore next */
const rawColumn = Utils.isString(column) ? column.split('.').map(e => this.knex.ref(e)).join('.') : column;
Expand Down
5 changes: 5 additions & 0 deletions tests/QueryBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2654,6 +2654,11 @@ describe('QueryBuilder', () => {
expect(sql).toBe(expected);
});

test(`sub-query order-by fields should not include 'as'`, async () => {
const sql = orm.em.createQueryBuilder(Author2).select('*').joinAndSelect('books', 'books').orderBy([{ books: { priceTaxed: QueryOrder.DESC } }, { id: QueryOrder.DESC }]).limit(10).getFormattedQuery();
expect(sql).toBe('select `e0`.*, `books`.`uuid_pk` as `books__uuid_pk`, `books`.`created_at` as `books__created_at`, `books`.`title` as `books__title`, `books`.`price` as `books__price`, `books`.price * 1.19 as `books__price_taxed`, `books`.`double` as `books__double`, `books`.`meta` as `books__meta`, `books`.`author_id` as `books__author_id`, `books`.`publisher_id` as `books__publisher_id` from `author2` as `e0` inner join `book2` as `books` on `e0`.`id` = `books`.`author_id` where `e0`.`id` in (select `e0`.`id` from (select `e0`.`id` from `author2` as `e0` inner join `book2` as `books` on `e0`.`id` = `books`.`author_id` group by `e0`.`id` order by min(`books`.price * 1.19) desc, min(`e0`.`id`) desc limit 10) as `e0`) order by `books`.price * 1.19 desc, `e0`.`id` desc');
});

afterAll(async () => orm.close(true));

});

0 comments on commit 74751fb

Please sign in to comment.