Available since 4.3.0.
enable in your schema file:
generator client {
provider = "prisma-client-js"
previewFeatures = ["filteredRelationCount"] << add this
}
and then query:
await prisma.post.findMany({
select: {
_count: {
select: {
comment: { where: { approved: true } },
},
},
},
})
Answer from eagor on Stack OverflowAvailable since 4.3.0.
enable in your schema file:
generator client {
provider = "prisma-client-js"
previewFeatures = ["filteredRelationCount"] << add this
}
and then query:
await prisma.post.findMany({
select: {
_count: {
select: {
comment: { where: { approved: true } },
},
},
},
})
You would need to use Raw Query to achieve this as the filter on _count for relations is not supported yet.
Here's the Feature Request for the same: Ability to filter count in "Count Relation Feature"
[Edit: 14-Nov-2022]
Prisma has added support for filteredRelationCount since version 4.3.0
Prisma - filter by related records count
How does one implement Prisma's features like count relations and nested reads?
Support aggregations (e.g. count) on nested relations
How do I get the count in nested relations.
case from my project - i have applicant and i can link to applicant family members.
i need to implement filter, which will return applicants, which have family member's count more than N.
after some research (https://github.com/prisma/prisma/issues/8935) that this feature still doesn't exist. how can i figure out from this case ?
__
im myself laravel guy, so in laravel's eloquent i would write something like that: