Skip to content

Commit 2083244

Browse files
pkudinovclaude
andauthored
fix(policy): handle DefaultInsertValueNode in createManyAndReturn (#2461)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 00c53d7 commit 2083244

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

packages/plugins/policy/src/policy-handler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,10 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
920920
for (let i = 0; i < data.length; i++) {
921921
const item = data[i]!;
922922
if (typeof item === 'object' && item && 'kind' in item) {
923+
if (item.kind === 'DefaultInsertValueNode') {
924+
result.push({ node: ValueNode.create(null), raw: null });
925+
continue;
926+
}
923927
const fieldDef = QueryUtils.requireField(this.client.$schema, model, fields[i]!);
924928
invariant(item.kind === 'ValueNode', 'expecting a ValueNode');
925929
result.push({
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createPolicyTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
4+
// createManyAndReturn fails with "Invariant failed: expecting a ValueNode"
5+
// when rows in the batch have asymmetric columns (one row provides a field the other omits)
6+
describe('Regression for issue #2460', () => {
7+
it('createManyAndReturn with asymmetric optional fields across rows', async () => {
8+
const db = await createPolicyTestClient(
9+
`
10+
model User {
11+
id Int @id @default(autoincrement())
12+
role String
13+
@@allow('all', true)
14+
}
15+
16+
model Item {
17+
id Int @id @default(autoincrement())
18+
key String
19+
note String?
20+
@@allow('all', auth().role == 'admin')
21+
}
22+
`,
23+
{ provider: 'postgresql' },
24+
);
25+
26+
const user = await db.user.create({ data: { role: 'admin' } });
27+
28+
const result = await db.$setAuth(user).item.createManyAndReturn({
29+
data: [
30+
{ key: 'a', note: 'hello' },
31+
{ key: 'b' },
32+
],
33+
});
34+
35+
expect(result).toHaveLength(2);
36+
});
37+
});

0 commit comments

Comments
 (0)