Policy expressions do not map `@map`-ed enum values in several comparison shapes
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 活跃
- 技术栈
- postgresql, sqlite, typescript
调研方向
Read the enum name mapper and policy transformer, then compare their handling of the listed policy expressions with the generated TypeScript schema’s enum representation. Reproduce the failing comparison and array cases on SQLite and PostgreSQL; done means mapped enum values work for those cases without breaking the listed passing cases.
由索引模型根据 Issue 内容生成。
描述
Summary
When an enum has @map-ed member values, the ORM's name mapper translates enum names to database values at SQL execution time by pattern-matching Kysely nodes (column = value, column IN (...), insert/update values, select projections). Several SQL shapes produced by the policy plugin never reach the mapper in a form it recognizes, so policies silently return no rows on SQLite or fail with type errors on PostgreSQL.
Follow-up to #2807, which fixed field in [A, B] and field in Enum for this case.
Schema used for all cases
enum PostStatus {
DRAFT @map('draft')
ACTIVE @map('active')
CANCELLED @map('cancelled')
}
model User {
id Int @id @default(autoincrement())
status PostStatus
posts Post[]
@@allow('all', true)
}
model Post {
id Int @id
status PostStatus
authorId Int
author User @relation(fields: [authorId], references: [id])
@@allow('create', true)
@@allow('read', <rule>)
}
Seed: one user with status: 'ACTIVE', three posts with DRAFT, ACTIVE, CANCELLED. Auth: { id: 1, status: 'ACTIVE' }.
Failing cases
1. Column on the right side of a comparison
ACTIVE == statusauth().status == status
Generated SQL is $1 = "Post"."status" with $1 = 'ACTIVE'. The mapper's binary-operation hook only handles a column reference on the left. SQLite returns no rows; PostgreSQL fails with invalid input value for enum "PostStatus": "ACTIVE".
2. Column vs. column through a relation
author.status == status
Generated SQL is (select CASE WHEN "$$t1"."status" = 'draft' THEN 'DRAFT' ... END from "User" as "$$t1" where ...) = "Post"."status". The mapper applies its mapped-back CASE projection inside the correlated subquery, so the left side yields the enum name while the right side is the raw database value. SQLite returns no rows; PostgreSQL fails with operator does not exist: text = "PostStatus".
Note that author.status == ACTIVE passes only because both sides happen to be names.
3. Enum array fields (PostgreSQL)
With statuses PostStatus[] on both User and Post:
ACTIVE in statusesauth().status in statusesstatus in auth().statusesCANCELLED in auth().statuses
These go through the = ANY(...) / array-contains paths which the mapper does not handle, and some also hit PostgreSQL type errors (No function matches the given name and argument types).
Passing cases (for reference)
status == ACTIVE, status != CANCELLED, status == auth().status, status in [DRAFT, ACTIVE], !(status in [...]), status in PostStatus, author.status == ACTIVE, @@deny with status == CANCELLED, all four collection-predicate forms, and status in [...] in create/update/post-update/delete policies.
Discussion
Case 1 is a small addition to the mapper (handle the mirrored shape). Cases 2 and 3 are harder to fix by pattern-matching SQL: the mapper cannot distinguish a subquery used as a result column from one used as a comparison operand.
An alternative worth considering: make the policy transformer own the rule that everything inside policy SQL is in database representation, mapping enum literals and auth() values on the way in and never emitting mapped-back CASE projections in policy subqueries. A dedicated enum-reference expression kind in the generated TS schema (instead of a plain string literal) would make that mapping exact. The mapper would keep its current scope: CRUD arguments and result projections.
- 主要语言
- TypeScript
- 星标
- 2.9k
- 派生
- 157
- 平均合并
- 2 天 7 小时
- 30 天内合并 PR
- 18
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
zenstackhq/zenstack 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
zenstackhq/zenstack#2873 ·
维护者通常 1 天内回复
-
`@default` on a field of a `@map`-ed enum emits the enum name as the column default可能重新可做 @jkugsiya 于 9 天前认领,目前没有进行中的 PR。 未关闭runtime
难度 2/5 1-3 小时 新手友好度 78/100
zenstackhq/zenstack#2868 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
zenstackhq/zenstack#2694 · 3 条评论 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 68/100
zenstackhq/zenstack#2659 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
zenstackhq/zenstack#2542 · 1 条评论 ·
维护者通常 1 天内回复
查看 zenstackhq/zenstack 的全部 Issue
相似的 Issue
-
native
难度 2/5 1-3 小时 新手友好度 72/100
prime-radiant-inc/evener#4839 ·
维护者通常 1 天内回复
-
bug needs-triage
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 74/100
processone/fluux-messenger#1790 ·
维护者通常 1 天内回复
-
🛣️ Infrastructure
难度 2/5 1-3 小时 新手友好度 65/100
opencrvs/opencrvs-core#14232 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
kunchenguid/gh-axi#220 ·
维护者通常 1 天内回复