Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

[Feature Request] One way relations without overhead

Đang mở
#2,681 1 bình luận 3 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
12/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
typescript
Lĩnh vực
database

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
2.9k
Fork
157
Merge trung bình
4 ngày 5 giờ
Pull request đã merge (30 ngày)
22

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của zenstackhq/zenstack

Tất cả issue của zenstackhq/zenstack

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.