Skip to content

fix(delegate): deleteMany fails when the model has compound id fields #2004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2025
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
21 changes: 19 additions & 2 deletions packages/runtime/src/enhancements/node/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1106,15 +1106,32 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
const entities = await db[model].findMany(findArgs);

// recursively delete base entities (they all have the same id values)
await Promise.all(entities.map((entity) => this.doDelete(db, model, { where: entity })));

await Promise.all(
entities.map((entity) => {
let deleteFilter = entity;
if (Object.keys(deleteFilter).length > 1) {
// if the model has compound id fields, we need to compose a compound key filter,
// otherwise calling Prisma's `delete` won't work
deleteFilter = this.queryUtils.composeCompoundUniqueField(model, deleteFilter);
}
return this.doDelete(db, model, { where: deleteFilter });
})
);

return { count: entities.length };
}

private async deleteBaseRecursively(db: CrudContract, model: string, idValues: any) {
let base = this.getBaseModel(model);
while (base) {
await db[base.name].delete({ where: idValues });
let deleteFilter = idValues;
if (Object.keys(idValues).length > 1) {
// if the model has compound id fields, we need to compose a compound key filter,
// otherwise calling Prisma's `delete` won't work
deleteFilter = this.queryUtils.composeCompoundUniqueField(base.name, deleteFilter);
}
await db[base.name].delete({ where: deleteFilter });
base = this.getBaseModel(base.name);
}
}
Expand Down
59 changes: 59 additions & 0 deletions tests/regression/tests/issue-1998.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('issue 1998', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model Entity {
id String @id
type String
updatable Boolean
children Relation[] @relation("children")
parents Relation[] @relation("parents")

@@delegate(type)
@@allow('create,read', true)
@@allow('update', updatable)
}

model A extends Entity {}

model B extends Entity {}

model Relation {
parent Entity @relation("children", fields: [parentId], references: [id])
parentId String
child Entity @relation("parents", fields: [childId], references: [id])
childId String

@@allow('create', true)
@@allow('read', check(parent, 'read') && check(child, 'read'))
@@allow('delete', check(parent, 'update') && check(child, 'update'))

@@id([parentId, childId])
}
`
);

const db = enhance();

await db.a.create({ data: { id: '1', updatable: true } });
await db.b.create({ data: { id: '2', updatable: true } });
await db.relation.create({ data: { parentId: '1', childId: '2' } });

await expect(
db.relation.deleteMany({
where: { parentId: '1', childId: '2' },
})
).resolves.toEqual({ count: 1 });

await db.a.create({ data: { id: '3', updatable: false } });
await db.b.create({ data: { id: '4', updatable: false } });
await db.relation.create({ data: { parentId: '3', childId: '4' } });
await expect(
db.relation.deleteMany({
where: { parentId: '3', childId: '4' },
})
).resolves.toEqual({ count: 0 });
});
});
Loading