question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve relation syntax for simple scenarios

See original GitHub issue

Problem

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:open
  • Created 3 years ago
  • Reactions:19
  • Comments:31 (11 by maintainers)

github_iconTop GitHub Comments

24reactions
Faithfindercommented, May 13, 2022

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.

20reactions
cannikincommented, Apr 24, 2020

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 User and a Post. 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 that Post has a userId. User contains no special database columns.

One possible version of describing this in schema.prisma with the minimal amount of information possible could be something like:

model User {
  id      Int  @id @default(autoincrement())
}

model Post {
  id      Int  @id @default(autoincrement())
  userId  User
}
  1. User itself has no special note about being related to Post. That can be inferred by the fact that Post has a userId of type User.
  2. Post.userId has a type of User, not Int. You can infer the actual database type by the fact that there is another model User and a foreign key will always* point to the id column of the other model, and you can get the type from there. So when you actually create the database schema you know that userId points to User and User.id is Int, therefore userId type is Int.
  3. When a foreign key relationship is found (Post.userId) you could automatically provide the posts Post[] declaration for the User so 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 User but the relation is named author then you could provide some syntax to override the defaults, similar to the @relation stuff 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. 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found