Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
Prisma exclude link table when fetching relation

Does anyone know if Prisma has a way to avoid unnecessary nesting levels when fetching nested objects through the link table?

Schema:

model User {
    id            String    @id @default(cuid())
    name          String?
    email         String?   @unique
    emailVerified DateTime?
    image         String?
    accounts      Account[]
    sessions      Session[]
    roles         UsersRoles[]
    created_at    DateTime  @default(now())
}

model Role {
    id    Int @id @default(autoincrement())
    name  AllowedRoles @default(USER)
    users UsersRoles[]
}

model UsersRoles {
    userId String
    user   User @relation(fields: [userId], references: [id])
    roleId Int
    role   Role @relation(fields: [roleId], references: [id])

    @@id([userId, roleId])
}

In the screenshot you see that I must do

include: {
    roles: {
        include: {
            role: true
        }
    }
}

to get the role id and name, but I also get the unnecessary data from the link table/pivot table. Can I query the role data directly, without getting the pivot table in the result? It works with Prisma implicit many to many relationships, but I want to do with explicit

enter image description here

Edit 1: Found this https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/working-with-many-to-many-relations Prisma shows an example how to get rid of link table data:

const result = posts.map((post) => {
  return { ...post, tags: post.tags.map((tag) => tag.tag) }
})

I guess since even in Prisma docs they do it like this, there is no way to do so during fetching time with Prisma client. But I might be wrong.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try swapping out your top-level include for a select. Prisma will return all scalar (non-relation) fields by default unless you specify a select object.

relevant docs

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!