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
11 changes: 11 additions & 0 deletions packages/plugins/policy/src/expression-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,17 @@ export class ExpressionTransformer<Schema extends SchemaDef> {
const tableName = context.alias ?? context.modelOrType;

const fieldDef = QueryUtils.requireField(this.schema, context.modelOrType, column);

if (fieldDef.computed) {
// `@computed` fields don't physically exist as columns — inline the
// computed expression the same way every other query path does via
// `dialect.fieldRef()`, instead of emitting a bare (nonexistent) column ref.
if (fieldDef.params) {
throw new Error(`parameterized computed field "${column}" cannot be used in a policy expression`);
}
return this.dialect.fieldRef(context.modelOrType, column, tableName).toOperationNode();
}

if (!fieldDef.originModel || fieldDef.originModel === context.modelOrType) {
return ReferenceNode.create(ColumnNode.create(column), TableNode.create(tableName));
}
Expand Down
34 changes: 34 additions & 0 deletions tests/regression/test/issue-2888.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createPolicyTestClient } from '@zenstackhq/testtools';
import { describe, expect, it } from 'vitest';

describe('Regression for issue 2888', () => {
it('a computed field referenced in @@allow does not throw "no such column" and evaluates its expression', async () => {
const db = await createPolicyTestClient(
`
model Model {
id Int @id @default(autoincrement())
x Int
canEdit Boolean @computed

@@allow('create,read', true)
@@allow('update', canEdit)
}
`,
{
provider: 'mysql',
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
usePrismaPush: true,
computedFields: {
model: {
canEdit: (eb: any) => eb.lit(true),
},
},
} as any,
);

await db.model.create({ data: { id: 1, x: 1 } });

await expect(db.model.update({ where: { id: 1 }, data: { x: 2 } })).resolves.toMatchObject({
x: 2,
});
});
});
Loading