Skip to content

Commit

Permalink
fix(query-builder): fix pagination when PK uses BigIntType
Browse files Browse the repository at this point in the history
Closes #4227
  • Loading branch information
B4nan committed Apr 18, 2023
1 parent 3a3b0bd commit b789031
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/utils/QueryHelper.ts
Expand Up @@ -237,6 +237,10 @@ export class QueryHelper {
return (cond as ObjectQuery<T>[]).map(v => QueryHelper.processCustomType(prop, v, platform, key, fromQuery)) as unknown as ObjectQuery<T>;
}

if (platform.isRaw(cond)) {
return cond;
}

return prop.customType.convertToDatabaseValue(cond, platform, { fromQuery, key, mode: 'query' });
}

Expand Down
2 changes: 2 additions & 0 deletions packages/knex/src/query/QueryBuilder.ts
Expand Up @@ -495,6 +495,7 @@ export class QueryBuilder<T extends object = AnyEntity> {
getKnexQuery(): Knex.QueryBuilder {
this.finalize();
const qb = this.getQueryBase();
(qb as Dictionary).__raw = true; // tag it as there is now way to check via `instanceof`

Utils.runIfNotEmpty(() => this.helper.appendQueryCondition(this.type ?? QueryType.SELECT, this._cond, qb), this._cond && !this._onConflict);
Utils.runIfNotEmpty(() => qb.groupBy(this.prepareFields(this._groupBy, 'groupBy')), this._groupBy);
Expand Down Expand Up @@ -1117,6 +1118,7 @@ export class QueryBuilder<T extends object = AnyEntity> {
// multiple sub-queries are needed to get around mysql limitations with order by + limit + where in + group by (o.O)
// https://stackoverflow.com/questions/17892762/mysql-this-version-of-mysql-doesnt-yet-support-limit-in-all-any-some-subqu
const subSubQuery = this.getKnex().select(pks).from(knexQuery);
(subSubQuery as Dictionary).__raw = true; // tag it as there is now way to check via `instanceof`
this._limit = undefined;
this._offset = undefined;
const cond = this._cond;
Expand Down
52 changes: 52 additions & 0 deletions tests/issues/GH4227.test.ts
@@ -0,0 +1,52 @@
import { Entity, LoadStrategy, OneToMany, ManyToOne, Collection, PrimaryKey, BigIntType } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/postgresql';

@Entity()
class BidEntity {

@PrimaryKey({ type: BigIntType })
id!: string;

@ManyToOne('ItemEntity', {
serializer: value => value.id,
serializedName: 'itemId',
})
item: any;

}

@Entity()
class ItemEntity {

@PrimaryKey({ type: BigIntType })
id!: string;

@OneToMany('BidEntity', 'item', {
orphanRemoval: true,
strategy: LoadStrategy.JOINED,
mappedBy: 'item',
})
bids = new Collection<BidEntity>(this);

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
entities: [BidEntity, ItemEntity],
});
await orm.schema.refreshDatabase();
});

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

test('select big int', async () => {
await orm.em
.createQueryBuilder(ItemEntity, 'item')
.select('*')
.leftJoinAndSelect('item.bids', 'bids')
.limit(10)
.getResultAndCount();
});

0 comments on commit b789031

Please sign in to comment.