Skip to content

Commit

Permalink
fix(core): respect * in partial loading with joined strategy
Browse files Browse the repository at this point in the history
Closes #3868
  • Loading branch information
B4nan committed Dec 25, 2022
1 parent e574924 commit 7781f84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,17 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
const fields: Field<T>[] = [];
const joinedProps = this.joinedProps(meta, populate);

if (explicitFields?.includes('*')) {
fields.push('*');
}

const shouldHaveColumn = <U>(prop: EntityProperty<U>, populate: PopulateOptions<U>[], fields?: Field<U>[]) => {
if (!this.platform.shouldHaveColumn(prop, populate)) {
return false;
}

if (!fields || prop.primary) {
return true;
return !fields?.includes('*');
}

return fields.includes(prop.name);
Expand Down
18 changes: 18 additions & 0 deletions tests/features/partial-loading/partial-loading.mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ describe('partial loading (mysql)', () => {
'left join `book2_tags` as `b2` on `b0`.`id` = `b2`.`book_tag2_id` ' +
'left join `book2` as `b1` on `b2`.`book2_uuid_pk` = `b1`.`uuid_pk` ' +
'left join `author2` as `a3` on `b1`.`author_id` = `a3`.`id`');

mock.mockReset();

const r2 = await orm.em.find(BookTag2, {}, {
fields: ['*', 'books.title', 'books.author', 'books.author.email'],
populate: ['books.author'],
filters: false,
strategy: LoadStrategy.JOINED,
});
expect(r2).toHaveLength(6);
expect(mock.mock.calls).toHaveLength(1);
expect(mock.mock.calls[0][0]).toMatch('select `b0`.*, ' +
'`b1`.`uuid_pk` as `b1__uuid_pk`, `b1`.`title` as `b1__title`, `b1`.`author_id` as `b1__author_id`, ' +
'`a3`.`id` as `a3__id`, `a3`.`email` as `a3__email` ' +
'from `book_tag2` as `b0` ' +
'left join `book2_tags` as `b2` on `b0`.`id` = `b2`.`book_tag2_id` ' +
'left join `book2` as `b1` on `b2`.`book2_uuid_pk` = `b1`.`uuid_pk` ' +
'left join `author2` as `a3` on `b1`.`author_id` = `a3`.`id`');
});

test('partial nested loading (object notation)', async () => {
Expand Down

0 comments on commit 7781f84

Please sign in to comment.