diff --git a/packages/orm/src/client/crud/dialects/postgresql.ts b/packages/orm/src/client/crud/dialects/postgresql.ts index 41d9b9006..25345ada0 100644 --- a/packages/orm/src/client/crud/dialects/postgresql.ts +++ b/packages/orm/src/client/crud/dialects/postgresql.ts @@ -409,7 +409,7 @@ export class PostgresCrudDialect extends LateralJoinDi } override buildArrayLength(array: Expression): AliasableExpression { - return this.eb.fn('array_length', [array]); + return this.eb.fn('array_length', [array, sql.lit(1)]); } override buildArrayValue(values: Expression[], elemType: string): AliasableExpression { diff --git a/tests/e2e/orm/policy/isempty-function-pg.test.ts b/tests/e2e/orm/policy/isempty-function-pg.test.ts new file mode 100644 index 000000000..9af038d02 --- /dev/null +++ b/tests/e2e/orm/policy/isempty-function-pg.test.ts @@ -0,0 +1,36 @@ +import { createPolicyTestClient } from '@zenstackhq/testtools'; +import { describe, expect, it } from 'vitest'; + +describe('isEmpty function in policies using Postgres', () => { + it('does not throw an error', async () => { + const schema = ` +model User { + id Int @id @default(autoincrement()) + roles Role[] + + @@allow('all', true) + @@deny('create', isEmpty(roles)) +} + +enum Role { + AUTHOR + EDITOR +} + `; + + const client = await createPolicyTestClient(schema, { + usePrismaPush: true, + provider: 'postgresql', + }); + + await expect(client.user.create({ + data: { + id: 1, + roles: ['AUTHOR'], + }, + })).resolves.toMatchObject({ + id: 1, + roles: ['AUTHOR'], + }); + }); +});