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

[Feature Request] One way relations without overhead

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
12/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
技术栈
typescript
领域
database

调研方向

This is a design proposal for ZenStack's schema language, not a scoped task: it asks that a relation be recognized implicitly and its back-relation fields omitted from the generated model, with the alternative of an explicit @relation(oneWay: true) attribute. Start by reading the Prisma-schema parsing and relation-resolution code paths in the ZenStack repo to see where back-relations are currently required, and review the referenced prisma/prisma#2018 thread for the long-standing constraints. Nothing is named in the issue itself, no test or file is pointed at, and the many-to-many case is explicitly left open, so scope has to be settled before any implementation begins.

由索引模型根据 Issue 内容生成。

描述

Problem

In many cases a user may want to create a one-way relationship between models, for example:

model User {
  id    Int    @id
  image Image?
}

model Product {
  slug  String @id
  image Image?
}

model Post {
  id     Int     @id
  images Image[]
}

model Image {
  id          String   @id
  path        String
  userId      Int?     @unique
  user        User?    @relation(fields: [userId], references: [id])
  productSlug String?  @unique
  product     Product? @relation(fields: [productSlug], references: [slug])
  postId      Int?
  post        Post?    @relation(fields: [postId], references: [id])
}

The image model is over-specified and causes not only headaches when designing models, but also later when referencing the data.
This issue has been a known limitation of Prisma for over 7 years and has over 100 comments as seen in #2018.

Proposed solution

Best solution would be to implicitly recognise relations, and if the user does not explicitly define a back-relation, then ZenStack should omit relation fields in the underlying model. This way, model from before would look like this:

model User {
  id    Int    @id
  image Image?
}

model Product {
  slug  String @id
  image Image?
}

model Post {
  id     Int     @id
  images Image[]
}

model Image {
  id          String   @id
  path        String
}

Thus, when referencing User we have access to Image, but when referencing Image we do not necessarily have references to User/Product/Post.

I do not see any side effects with this solution and it should not introduce any breaking changes. If some were to come up during development, ZenStack could implement some attribute, like @relation(oneWay: true) to make sure the user knows what they're doing and explicitly configure the model with one way relation.

Alternatives

There really aren't any good alternatives, as seen in Prisma issue referenced earlier, the best solution are janky TypeScript types like Omit or as any all over the place, which introduce bugs and break with every change to the model. These also does not remove the problem of very long model definitions.

Additional considerations

It would also be great if it were possible to define explicit many-to-many relations one way. As far as I'm aware, my solution would be a drop-in replacement and should work out-of-the-box, but this may need to be additionally though through.

主要语言
TypeScript
星标
2.9k
派生
157
平均合并
4 天 5 小时
30 天内合并 PR
22

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

zenstackhq/zenstack 的其他 Issue

查看 zenstackhq/zenstack 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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