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 the Schema Relation API

See original GitHub issue

The 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])
}
  1. Simpler because the entire Post to User relation is 1 line instead of 2 lines.
  2. 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[])
  3. Then the whole “Link field” concept can be removed (This is a confusing concept, even for people like me who really understand DB design)
  4. Compound foreign keys relations can instead be handled by adding @relation on a @@unique definition like @@unique([field1, field2]) @relation(...)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Syttencommented, Mar 28, 2020

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.

1reaction
janpiocommented, Mar 30, 2020

The wish to improve and simplify the API still is valid and correct - so we can keep this open.

Read more comments on GitHub >

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

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