Skip to content
Open
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
7 changes: 7 additions & 0 deletions packages/orm/src/client/executor/zenstack-query-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ export class ZenStackQueryExecutor extends DefaultQueryExecutor {
? this.getInsertIds(mutationInfo.model, queryNode, queryResult)
: preUpdateIds;

if (mutatedIds && mutatedIds.length === 0) {
// zero rows matched (e.g. a policy-denied or genuinely empty
// updateMany/deleteMany) — nothing to reload, and NOT the "can't
// determine ids at all" case below, so don't warn.
return [];
}

if (mutatedIds) {
const idFields = requireIdFields(this.client.$schema, mutationInfo.model);
const eb = expressionBuilder<any, any>();
Expand Down
29 changes: 29 additions & 0 deletions tests/regression/test/issue-2884.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createTestClient } from '@zenstackhq/testtools';
import { describe, expect, it } from 'vitest';

describe('Regression for issue 2884', () => {
it('zero-match updateMany on MySQL does not throw when an afterEntityMutation hook is registered', async () => {
const db = await createTestClient(
`
model User {
id Int @id @default(autoincrement())
email String @unique
}
`,
{ provider: 'mysql', usePrismaPush: true },
);

const hooked = db.$use({
id: 'noop',
onEntityMutation: {
afterEntityMutation: async () => {},
},
});

// no row with id 999 exists -> preUpdateIds resolves to [] -> should be a
// normal zero-match update, not a thrown SQL syntax error
await expect(
hooked.user.updateMany({ where: { id: 999 }, data: { email: 'x@example.com' } }),
).resolves.toEqual({ count: 0 });
});
});
Loading