Skip to content

Commit

Permalink
fix(postgres): fix pagination with order by UUID PK
Browse files Browse the repository at this point in the history
Closes #2910
  • Loading branch information
B4nan committed Mar 14, 2022
1 parent eba497d commit 042626c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
7 changes: 7 additions & 0 deletions packages/core/src/platforms/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,11 @@ export abstract class Platform {
throw new Error('Not supported');
}

/**
* @internal
*/
castColumn(prop?: EntityProperty): string {
return '';
}

}
5 changes: 4 additions & 1 deletion packages/knex/src/query/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,11 @@ export class QueryBuilder<T extends AnyEntity<T> = AnyEntity> {
const orderBy = [];
for (const orderMap of this._orderBy) {
for (const [field, direction] of Object.entries(orderMap)) {
const [a, f] = this.helper.splitField(field);
const prop = this.helper.getProperty(f, a);
const type = this.platform.castColumn(prop);
orderBy.push({
[`min(${this.ref(this.helper.mapper(field, this.type))})`]: direction,
[`min(${this.ref(this.helper.mapper(field, this.type))}${type})`]: direction,
});
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/postgresql/src/PostgreSqlPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,15 @@ export class PostgreSqlPlatform extends AbstractSqlPlatform {
return indexName;
}

/**
* @inheritDoc
*/
castColumn(prop?: EntityProperty): string {
if (prop?.columnTypes?.[0] === this.getUuidTypeDeclarationSQL({})) {
return '::text';
}

return '';
}

}
12 changes: 8 additions & 4 deletions tests/features/paginate-flag.postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Collection, Entity, Enum, Filter, ManyToMany, ManyToOne, MikroORM, OneToMany, OneToOne, PrimaryKey, Property } from '@mikro-orm/core';
import type { PostgreSqlDriver } from '@mikro-orm/postgresql';
import { v4 } from 'uuid';

@Entity()
export class Group {
Expand Down Expand Up @@ -85,8 +86,8 @@ export class C {
@Entity()
export class T {

@PrimaryKey()
id!: number;
@PrimaryKey({ type: 'uuid' })
id: string = v4();

@Property()
end!: Date;
Expand All @@ -106,8 +107,8 @@ export class T {
@Entity()
export class TC {

@PrimaryKey()
id!: number;
@PrimaryKey({ type: 'uuid' })
id: string = v4();

@ManyToOne(() => C, { nullable: true })
c?: C;
Expand Down Expand Up @@ -252,6 +253,9 @@ describe('GH issue 2095', () => {
expect(firstResults.length).toBe(20);
expect(secondResults.length).toBe(20);
expect(firstMaxDate.getTime()).toBeLessThan(secondMinDate.getTime());

// this requires casting of the UUID column to use it in `min()` (GH #2910)
await orm.em.fork().find(T, {}, { orderBy: { tc: { id: 'desc' } }, limit: 5 });
});

test('getting users with limit 3. must be: [id-user-03, id-user-02, id-user-01]', async () => {
Expand Down

0 comments on commit 042626c

Please sign in to comment.