question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to filter count in "Count Relation Feature"

See original GitHub issue

Problem

Currently count relation feature just accepts true or false values and count all the related records. Sometimes we just need to count some of the records depends on the query.

For example

this.prisma.groupExamAttempt.findMany({
  where: {
    groupId: param.groupId,
    examId: param.examId,
  },
  include: {
    _count: {
      select: {
        answers: true,
      },
    },
  },
});

This will just passively count all the records there.

Suggested solution

To filter maybe we can add a where clause

this.prisma.groupExamAttempt.findMany({
  where: {
    groupId: param.groupId,
    examId: param.examId,
  },
  include: {
    _count: {
      select: {
        answers: {
          where: {
            isCorrect: true,
          },
        },
      },
    },
  },
});

Maybe if applicable, It can also be based on the deeper relationship filter. (IF ONLY THIS IS DOABLE)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:78
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

16reactions
dickficklingcommented, Aug 19, 2021

Running into a need for this too. Would love to be able to do something like e.g. find users with >50 followers

prisma.user.findMany({ where: { followers: { _count: { gt: 50 } } } });
11reactions
dariusj18commented, Aug 22, 2021

This is also pretty essential for soft deletes. Ex. no reason for deleted records to be included in the count.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Count filter use relationship - Microsoft Power BI Community
I have opportunity records that have a primary contact. ... Measure = CALCULATE ( COUNT ( 'Campaign Responses'[Campaign Name] ), FILTER ...
Read more >
How do I perform a count on a relation with a where clause in ...
Here's the Feature Request for the same: Ability to filter count in "Count Relation Feature". [Edit: 14-Nov-2022].
Read more >
How to Filter Records with Aggregate Function COUNT
To filter records according the given number of rows in the group, use the HAVING clause. It filters rows using in a condition...
Read more >
Count rows in a filtered list - Honeycode Community
The summary table solution will allow you to show a count for each name and filter out any count that is 0. The...
Read more >
COUNT() SQL FUNCTION - DataCamp
COUNT () , used with HAVING , is useful for filtering groups according to the number of rows they have. We will illustrate...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found