Say I have this prisma schema with an implicit m:n-relation of Post and Tag
model Post {
id String @id
tags Tag[]
}
model Tag {
id Int @id @default(autoincrement())
posts Post[]
}
How do I find the first Post that has no associated Tags?
prisma.post.findFirst({
where: {
tags: {
// are nonexistent (something like count === 0?)
},
},
}),
Thanks for the help 🙂
>Solution :
You can probably use orderBy by count of tags in ascending order and get the first one? Like that:
prisma.post.findFirst({
orderBy: { tags: { _count: 'asc' } },
});