How to get the count of a relationship using Prisma
Example Problem
Imagine we want to build a page to display all our posts including the number of comments on each post. Something like this…
Prisma Schema
Let’s start off by defining a Prisma schema that we can use in our example. We’ll create a Post and Comment table. Each post can have many comments on it.
Code
You can use the include property and the _count property inside of include to the get the count of any relationships.
This will add a _count property on your query results where you can get the count of the comments.