Hey folks, i was wondering if anyone knows how I c...
# orm-help
k
Hey folks, i was wondering if anyone knows how I could pull off a count that factors in a relation? say user and posts. e.g count how many users have posts?
1
n
Hey Kyler 👋 Can you have a look at this example - Count Relations? Query:
Copy code
const usersWithCount = await prisma.user.findMany({
  include: {
    _count: {
      select: { posts: true },
    },
  },
})
Response:
Copy code
{ id: 1, _count: { posts: 3 } },
{ id: 2, _count: { posts: 2 } },
{ id: 3, _count: { posts: 2 } },
{ id: 4, _count: { posts: 0 } },
{ id: 5, _count: { posts: 0 } }
👌 1
k
So i would then have to filter this to get only those which have posts > 0
n
Yes, you could filter this response to get users who have posts > 0