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

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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With