Skip to content

Commit

Permalink
fix(query-builder): fix cloning of alias map
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Mar 3, 2024
1 parent e005cc2 commit 50d8fb9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ export class QueryBuilder<T extends object = AnyEntity> {
RawQueryFragment.cloneRegistry = this.rawFragments;

for (const prop of Object.keys(this)) {
if (reset.includes(prop)) {
if (reset.includes(prop) || prop === '_helper') {
continue;
}

Expand All @@ -972,6 +972,7 @@ export class QueryBuilder<T extends object = AnyEntity> {
}

qb._aliases = { ...this._aliases };
(qb._helper as Dictionary).aliasMap = qb._aliases;
qb.finalized = false;

return qb;
Expand Down
14 changes: 14 additions & 0 deletions tests/features/collection/populate-where-infer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,17 @@ test('invalid query', async () => {
expect(res[0]).toHaveLength(1);
expect(res[1]).toBe(1);
});

test('invalid query 2', async () => {
const query = orm.em.createQueryBuilder(User, 'user')
.clone()
.where({
location: {
location: 'loc name',
},
});

expect(query.getFormattedQuery()).toBe(`select "user".* from "user" as "user" left join "location" as "l1" on "user"."location_id" = "l1"."id" where "l1"."location" = 'loc name'`);
const res = await query;
expect(res).toHaveLength(1);
});

0 comments on commit 50d8fb9

Please sign in to comment.