prisma / prisma/orm

Improve relation syntax for simple scenarios

Open
#2,018 77 comments 70 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kind/feature topic: schema
Dominant language
TypeScript
Stars
47.6k
Forks
2.5k
Avg merge
21h 59m
Merged PRs (30d)
95

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the Prisma schema relation examples and the discussion around relation scalar fields. The issue presents two possible directions—inference or formatter support—without selecting one. Done would require an agreed approach that makes simple relations less verbose while preserving correct relation information.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.