Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Policy expressions do not map `@map`-ed enum values in several comparison shapes

未关闭
#2,867 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
postgresql, sqlite, typescript
领域
authorization

调研方向

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 内容生成。

描述

plugin runtime

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 == status
  • auth().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 statuses
  • auth().status in statuses
  • status in auth().statuses
  • CANCELLED 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,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

zenstackhq/zenstack 的其他 Issue

查看 zenstackhq/zenstack 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。