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.

Ability to jump "through" one model to another on M:N relationships

See original GitHub issue

Problem

Once again this idea is inspired by Ruby on Rails. 😉 ActiveRecord (the ORM library that ships with Rails) has the concept of a “has many through” relationship. That is, one record has many associated records through another record inbetween.

For example, let’s say you have a Car which gets regular Service. The record of that car getting a service is handled by the Maintenance model, which records which car, which service, the date it was performed, and maybe the employee who performed the job. We could model this in schema.prisma like so (thanks to @pantharshit00 on Slack for working up the schema):

model Car {
  id          Int        @default(autoincrement()) @id
  make        String
  model       String
  year        Int
  maintenance Maintenance[]
}

model Service {
  id          Int        @default(autoincrement()) @id
  name        String
  maintenance Maintenance[]
}

model Employee {
  id              Int        @default(autoincrement()) @id
  name            String
  maintenanceDone Maintenance[]
}

model Maintenance {
  id          Int      @default(autoincrement()) @id
  car         Car      @relation(fields: [carId], references: [id])
  service     Service  @relation(fields: [serviceId], references: [id])
  employee    Employee @relation(fields: [employeeId], references: [id])
  carId       Int
  serviceId   Int
  employeeId  Int
  performedAt DateTime @default(now())
}

Getting the Maintenance records on a Car is pretty easy:

prisma.car.findOne({ where: { id: 123 } }).maintenance()

But what if I wanted the Service records associated with the car? Right now it would be something like:

prisma.car.findMany({
    include: {
      maintenance: {
        select: {
          service: true,
        },
      },
    },
  })

But what if you could simplify that syntax with the idea of the “has many through” relationship relationship: A Car has many Service records through Maintenance records.

It would be amazing if you could get to those records using the same, simple syntax:

prisma.car.findOne({ where: { id: 123 } }).service()

Solution

Perhaps an extra parameter can be passed to @relation somehow denoting that the records are reached through another? You give it the name of another field you’ve already defined on the model, like maintenance:

model Car {
  id          Int        @default(autoincrement()) @id
  make        String
  model String
  year Int
  maintenance Maintenance[]
  service Service[] @relation(through: maintenance)
}

Since we already have the definition of how a Service relates to a Maintenance in the Maintenance model there’s no need to repeat the other relation arguments.

Additional context

Here’s some Rails documentation on has-many-through: https://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

What do you think? Is it crazy enough to work?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:85
  • Comments:10

github_iconTop GitHub Comments

21reactions
mattiamalonnicommented, Jul 21, 2022

Just to keep thread alive… This would be an awesome feature to have. Also, really like the proposal approach.

13reactions
kenchamberscommented, Sep 3, 2021

we want has many through as well!

Read more comments on GitHub >

github_iconTop Results From Across the Web

One-To-One and Many-to-Many Database Relationships
Those relationships are defined through data. ... It's easy to jump to a conclusion based on one's perception of how a business operates....
Read more >
How to manage Many-To-Many relationships in a Data Model?
Power BI Weekly Videos. How to manage Many-To-Many relationships in a Data Model ? |Data Modeling Tutorial |BI Consulting Pro.
Read more >
another option for many to many relationships in Power BI ...
Analyze and measure the performance of an alternative data modeling solution suggested by Daniel Otykier (author of Tabular Editor) ...
Read more >
python - Relationship to "jump" through M2M ... - Stack Overflow
I am trying to model a very typical permission ...
Read more >
How to Implement Many-to-Many Relationships in Relational ...
Nearly any data model of non-negligible complexity will include at least one (if not several) many-to-many relationships.
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