Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/orm/src/client/crud/dialects/base-dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,17 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
const [field, order] = orderByItems[j]!;
const _order = negateOrderBy ? (order === 'asc' ? 'desc' : 'asc') : order;
const op = j === i ? (_order === 'asc' ? '>=' : '<=') : '=';
// Fields inherited from a delegate base live on the base table, which is
// joined with its model name as alias. See buildSelectModel/buildDelegateJoin.
const fieldDef = requireField(this.schema, model, field);
const outerAlias = fieldDef.originModel ?? modelAlias;
const subSelectAlias = fieldDef.originModel ?? subQueryAlias;
andFilters.push(
this.eb(
this.eb.ref(`${modelAlias}.${field}`),
this.eb.ref(`${outerAlias}.${field}`),
op,
this.buildSelectModel(model, subQueryAlias)
.select(`${subQueryAlias}.${field}`)
.select(`${subSelectAlias}.${field}`)
.where(cursorFilter),
),
);
Expand Down
78 changes: 78 additions & 0 deletions tests/regression/test/issue-2588.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { createTestClient } from '@zenstackhq/testtools';
import { describe, expect, it } from 'vitest';

// https://github.com/zenstackhq/zenstack/issues/2588
describe('Regression for issue 2588', () => {
const schema = `
model Asset {
id String @id @default(uuid())
createdAt DateTime @default(now())
assetType String
@@delegate(assetType)
}

model Notification extends Asset {
title String
}
`;

async function setup() {
const db = await createTestClient(schema);
const a = await db.notification.create({
data: { title: 'A', createdAt: new Date('2025-01-01T00:00:00Z') },
});
const b = await db.notification.create({
data: { title: 'B', createdAt: new Date('2025-01-02T00:00:00Z') },
});
const c = await db.notification.create({
data: { title: 'C', createdAt: new Date('2025-01-03T00:00:00Z') },
});
return { db, a, b, c };
}

it('cursor + orderBy on delegate parent field does not error', async () => {
const { db, b } = await setup();

const result = await db.notification.findMany({
cursor: { id: b.id },
orderBy: { createdAt: 'asc' },
});

expect(result.map((n: any) => n.title)).toEqual(['B', 'C']);
});

it('cursor + skip + orderBy on delegate parent field works', async () => {
const { db, a } = await setup();

const result = await db.notification.findMany({
cursor: { id: a.id },
skip: 1,
orderBy: { createdAt: 'asc' },
take: 25,
});

expect(result.map((n: any) => n.title)).toEqual(['B', 'C']);
});

it('cursor + multiple orderBy mixing child and delegate fields', async () => {
const { db, a } = await setup();

const result = await db.notification.findMany({
cursor: { id: a.id },
orderBy: [{ createdAt: 'asc' }, { id: 'asc' }],
});

expect(result.map((n: any) => n.title)).toEqual(['A', 'B', 'C']);
});

it('cursor + orderBy on child field still works', async () => {
const { db, b } = await setup();

const result = await db.notification.findMany({
cursor: { id: b.id },
orderBy: { title: 'asc' },
});

expect(result.map((n: any) => n.title)).toEqual(['B', 'C']);
});
});
Loading