'Query many-to-many relationship in Prisma
I want to query a many-to-many relation in prisma, like "select all posts where the category ID equals 'abc...' ".
This seems fairly simple but even after spending 2 hours reading the Prisma docs on relational queries, I can't figure it out.
model Category {
id String @id @default(cuid())
name String
post Post[]
}
model Post {
id String @id @default(cuid())
body String
category Category[]
}
const posts = await prisma.post.findMany({
select: {
category: {
where: {id: "abc123"}
}},
});
this returns an array of as many category objects as there are posts.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
