Improve relation syntax for simple scenarios
See original GitHub issueProblem
Right now, defining a relation in the Prisma schema is quite verbose due to the required relation scalar field:
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 // relation scalar field
}
~In implicit m-n-relations, we also need to use the @relation attribute:~ (You don’t need them now)
model Post {
id Int @id @default(autoincrement())
categories Category[] @relation(references: [id])
}
model Category {
id Int @id @default(autoincrement())
posts Post[] @relation(references: [id])
}
Solution
Maybe we can figure out a way how Prisma can infer the information that’s currently made explicit in the schema. Another solution could be to add a feature to the Prisma schema formatter which can insert the required fields automatically.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:19
- Comments:31 (11 by maintainers)
Top Results From Across the Web
Relations (Reference) - Prisma
Annotated relation fields ... Relations that require one side of the relation to be annotated with the @relation attribute are referred to as...
Read more >Syntax in the English Language: Definition, Examples, and 3 ...
Syntax is the set of rules that helps readers and writers make sense of ... Simple sentences consist of a single, independent clause....
Read more >Designing a Relational Database and Creating an Entity ...
In this article we have introduced the basic ideas of what a relational database is and how it works, discussed some of the...
Read more >Learn SQL: SQL Query examples - SQLShack
The goal of this article is to start with a fairly simple query and move towards more complex queries. We'll examine queries you...
Read more >What Is Syntax? Learn the Meaning and Rules, with Examples
Find out the definition, basic rules, and types of syntax so you can communicate effectively, including some syntax examples.
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

Just chiming in with my support for #3045
Often enough I don’t want to specify the other side of the relationship if it won’t ever be used - just makes the schema bloated. I don’t really understand why it’s required in the first place.
You guys should take a page out of Rails’s playbook, they’re pretty elegant when it comes to defining relations like this with the user doing the least amount of manual definitions as possible. It assumes a lot of defaults, which makes the syntax extremely concise in 90% of cases. If you have some custom field names then the syntax is provided to annotate those. But convention over configuration works great for the majority of cases! A good explanation here: https://guides.rubyonrails.org/association_basics.html#the-types-of-associations
The basics are that relation names are standardized. Let’s say you have a
Userand aPost. The post belongs to the user (and the user has many posts). When you have this kind of belongs-to relationship, the foreign key goes in the record that belongs to the other, always. “A post belongs to a user” means thatPosthas auserId.Usercontains no special database columns.One possible version of describing this in schema.prisma with the minimal amount of information possible could be something like:
Useritself has no special note about being related toPost. That can be inferred by the fact thatPosthas auserIdof typeUser.Post.userIdhas a type ofUser, notInt. You can infer the actual database type by the fact that there is another modelUserand a foreign key will always* point to theidcolumn of the other model, and you can get the type from there. So when you actually create the database schema you know thatuserIdpoints toUserandUser.idisInt, thereforeuserIdtype isInt.Post.userId) you could automatically provide theposts Post[]declaration for theUserso they don’t have to explicitly create it.* “always” really just means the default, you can provide options to override
In the example case above where the model is named
Userbut the relation is namedauthorthen you could provide some syntax to override the defaults, similar to the@relationstuff you have now.So it turns out that the schema is really just the database schema—the relations between them are inferred by the syntax, but could be embellished by hand if needed.
I can describe the other relations as well (has one, has many, many to many [which is really just a tweaked version of has many]) but that doc linked above does a pretty good job. The Rails philosophy is usually to have the user manually define as little as possible in most cases, which makes your life much easier. 😃