Skip to content

Commit

Permalink
fix(query-builder): check for duplicate selects when wrapping paginat…
Browse files Browse the repository at this point in the history
…ion query
  • Loading branch information
B4nan committed Mar 3, 2024
1 parent aacfbe6 commit e005cc2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 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 @@ -1470,7 +1470,7 @@ export class QueryBuilder<T extends object = AnyEntity> {
const type = this.platform.castColumn(prop);
const fieldName = this.helper.mapper(field, this.type, undefined, null);

if (!prop?.persist && !prop?.formula) {
if (!prop?.persist && !prop?.formula && !pks.includes(fieldName)) {
addToSelect.push(fieldName);
}

Expand Down
32 changes: 19 additions & 13 deletions tests/features/collection/populate-where-infer.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property, Ref } from '@mikro-orm/sqlite';
import { Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property, Ref } from '@mikro-orm/postgresql';
import { mockLogger } from '../../helpers';

@Entity()
Expand Down Expand Up @@ -47,10 +47,10 @@ let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
dbName: 'coll-operators-1',
entities: [User],
});
await orm.schema.createSchema();
await orm.schema.refreshDatabase();

orm.em.create(User, {
id: 1,
Expand Down Expand Up @@ -91,10 +91,7 @@ test('$every without populateWhere', async () => {
},
);
expect(res).toHaveLength(0);
expect(mock.mock.calls[0][0]).toMatch('select `u0`.*, `s1`.`id` as `s1__id`, `s1`.`name` as `s1__name`, `s1`.`user_id` as `s1__user_id` ' +
'from `user` as `u0` ' +
'left join `server` as `s1` on `u0`.`id` = `s1`.`user_id` ' +
'where `u0`.`id` not in (select `u0`.`id` from `user` as `u0` inner join `server` as `s1` on `u0`.`id` = `s1`.`user_id` where not (`s1`.`name` != \'test\'))');
expect(mock.mock.calls[0][0]).toMatch(`select "u0".*, "s1"."id" as "s1__id", "s1"."name" as "s1__name", "s1"."user_id" as "s1__user_id" from "user" as "u0" left join "server" as "s1" on "u0"."id" = "s1"."user_id" where "u0"."id" not in (select "u0"."id" from "user" as "u0" inner join "server" as "s1" on "u0"."id" = "s1"."user_id" where not ("s1"."name" != 'test'))`);
});

test('$every with populateWhere: infer', async () => {
Expand All @@ -117,15 +114,10 @@ test('$every with populateWhere: infer', async () => {
},
);
expect(res).toHaveLength(0);
expect(mock.mock.calls[0][0]).toMatch('select `u0`.*, `s1`.`id` as `s1__id`, `s1`.`name` as `s1__name`, `s1`.`user_id` as `s1__user_id` ' +
'from `user` as `u0` ' +
'left join `server` as `s1` on `u0`.`id` = `s1`.`user_id` ' +
'where `u0`.`id` not in (select `u0`.`id` from `user` as `u0` inner join `server` as `s1` on `u0`.`id` = `s1`.`user_id` where not (`s1`.`name` != \'test\'))');
expect(mock.mock.calls[0][0]).toMatch(`select "u0".*, "s1"."id" as "s1__id", "s1"."name" as "s1__name", "s1"."user_id" as "s1__user_id" from "user" as "u0" left join "server" as "s1" on "u0"."id" = "s1"."user_id" where "u0"."id" not in (select "u0"."id" from "user" as "u0" inner join "server" as "s1" on "u0"."id" = "s1"."user_id" where not ("s1"."name" != 'test'))`);
});

test('disallow $every on top level', async () => {
const mock = mockLogger(orm);

await expect(orm.em.fork().find(
User,
{
Expand All @@ -143,3 +135,17 @@ test('disallow $every on top level', async () => {
},
)).rejects.toThrow('Collection operators can be used only inside a collection property context, but it was used for User.id.');
});

test('invalid query', async () => {
const res = await orm.em.fork()
.createQueryBuilder(User, 'user')
.leftJoinAndSelect('user.servers', 'test')
.select('user.id')
.orderBy({
id: 'DESC',
})
.limit(1)
.getResultAndCount();
expect(res[0]).toHaveLength(1);
expect(res[1]).toBe(1);
});

0 comments on commit e005cc2

Please sign in to comment.