Skip to content

Commit

Permalink
fix(query-builder): do not ignore unmatching partial loading hints in…
Browse files Browse the repository at this point in the history
… `qb.joinAndSelect`

Closes #5445
  • Loading branch information
B4nan committed Apr 10, 2024
1 parent da68503 commit ccdf018
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
17 changes: 17 additions & 0 deletions packages/knex/src/query/QueryBuilder.ts
Expand Up @@ -313,6 +313,23 @@ export class QueryBuilder<T extends object = AnyEntity> {
populate.push(...children);
}

for (const p of prop.targetMeta!.getPrimaryProps()) {
fields.push(...this.driver.mapPropToFieldNames<T>(this, p, alias));
}

if (explicitFields) {
for (const field of explicitFields) {
const [a, f] = this.helper.splitField(field as EntityKey<T>);
const p = prop.targetMeta!.properties[f];

if (p) {
fields.push(...this.driver.mapPropToFieldNames<T>(this, p, alias));
} else {
fields.push(`${a}.${f} as ${a}__${f}`);
}
}
}

prop.targetMeta!.props
.filter(prop => explicitFields
? explicitFields.includes(prop.name) || explicitFields.includes(`${alias}.${prop.name}`) || prop.primary
Expand Down
Expand Up @@ -157,7 +157,7 @@ describe('partial loading via `exclude` (mysql)', () => {
expect(r3[0].books[0].title).toBe('Bible 1');
expect(r3[0].books[0].price).toBeUndefined();
expect(r3[0].books[0].author).toBeDefined();
expect(mock.mock.calls[0][0]).toMatch('select `a`.`id`, `b`.`uuid_pk` as `b__uuid_pk`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id` from `author2` as `a` inner join `book2` as `b` on `a`.`id` = `b`.`author_id` where `a`.`id` = ? order by `b`.`title` asc');
expect(mock.mock.calls[0][0]).toMatch('select `a`.`id`, `b`.`uuid_pk` as `b__uuid_pk`, `b`.`author_id` as `b__author_id`, `b`.`title` as `b__title` from `author2` as `a` inner join `book2` as `b` on `a`.`id` = `b`.`author_id` where `a`.`id` = ? order by `b`.`title` asc');
orm.em.clear();
mock.mock.calls.length = 0;
});
Expand Down
Expand Up @@ -160,7 +160,7 @@ describe('partial loading (mysql)', () => {
expect(r3[0].books[0].title).toBe('Bible 1');
expect(r3[0].books[0].price).toBeUndefined();
expect(r3[0].books[0].author).toBeDefined();
expect(mock.mock.calls[0][0]).toMatch('select `a`.`id`, `b`.`uuid_pk` as `b__uuid_pk`, `b`.`title` as `b__title`, `b`.`author_id` as `b__author_id` from `author2` as `a` inner join `book2` as `b` on `a`.`id` = `b`.`author_id` where `a`.`id` = ? order by `b`.`title` asc');
expect(mock.mock.calls[0][0]).toMatch('select `a`.`id`, `b`.`uuid_pk` as `b__uuid_pk`, `b`.`author_id` as `b__author_id`, `b`.`title` as `b__title` from `author2` as `a` inner join `book2` as `b` on `a`.`id` = `b`.`author_id` where `a`.`id` = ? order by `b`.`title` asc');
orm.em.clear();
mock.mock.calls.length = 0;

Expand Down
4 changes: 2 additions & 2 deletions tests/issues/GH5445.test.ts
Expand Up @@ -159,8 +159,8 @@ test('define joined columns in leftJoinAndSelect()', async () => {
},
[
'comments.content',
'comments.createdAt',
'comments.updatedAt',
'comments.created_at',
'comments.updated_at',
],
)
.where({ id: 5 })
Expand Down

0 comments on commit ccdf018

Please sign in to comment.