Improve the Schema Relation API
See original GitHub issueThe latest schema relation API design is the following:
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
}
I would like to propose a simpler, more intuitive alternative:
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
authorId Int @relation("author", model: User, references: [id])
}
- Simpler because the entire
Post
toUser
relation is 1 line instead of 2 lines. - More intuitive because it’s immediately clear which is physical vs which is virtual. A physical field always has a scalar type (
authorId Int
) and virtual field is always a model (posts Post[]
) - Then the whole “Link field” concept can be removed (This is a confusing concept, even for people like me who really understand DB design)
- Compound foreign keys relations can instead be handled by adding
@relation
on a@@unique
definition like@@unique([field1, field2]) @relation(...)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:5 (4 by maintainers)
Top Results From Across the Web
How to build a good API: Relationships and endpoints
In first blog post we wrote about how to design a good API. Now we are going to take the next step and...
Read more >Define a Relationship Between Two Schemas Using the ...
This document provides a tutorial for defining a one-to-one relationship between two schemas defined by your organization using the Schema ...
Read more >Relations (Reference) - Prisma
A relation is a connection between two models in the Prisma schema. This page explains how you can define one-to-one, one-to-many and many-to-many...
Read more >GraphQL relationships - Fauna Documentation
During schema import, the GraphQL API creates the necessary collections and indexes to correlate the data based on the recognized relationships in the...
Read more >Relational Schema - an overview | ScienceDirect Topics
The purpose of defining this metadata is to allow applications to access data using OLAP API or BI Beans. These APIs require that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I agree the syntax is nicer but we need a way to specify the name of the virtual relation when you do something like
prisma.post.findOne().author()
. Edit: I realized its the first parameter, ignore comment.The wish to improve and simplify the API still is valid and correct - so we can keep this open.