Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda