Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter on count of relations #23949

Closed
OllieJennings opened this issue Apr 24, 2024 · 3 comments
Closed

Filter on count of relations #23949

OllieJennings opened this issue Apr 24, 2024 · 3 comments
Labels
kind/feature A request for a new feature. team/client Issue for team Client. topic: _count https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count- topic: filteredRelationCount topic: relations

Comments

@OllieJennings
Copy link

Problem

Lets image the following models

model User {
  id String @id @default(cuid())
  name String

  followers Follower[] @relation('userFollowers')
}

model Follower {
  id String @id @default(cuid())
  following User @relation('userFollowers', field: [followingId], references: [id])
  followingId String
}

Now saying if l am trying to show the following: Give me all users who have greater than 50 followers

I want to be able to do this in a single findMany query

Suggested solution

This is the type of query l would want to run:

await prisma.user.findMany({
  where: {
    followers: {
       _count: {
        gt: 50
      }
    }
  }
});

Alternatives

I have looked at the groupBy and aggregate methods, but none allow me to achieve what l want above.

Annoyling l have had to use rawSQL which means l need to fully implement the whole take / include / where functionality by hand, so this would be a big bonus to that.

Additional context

These type of filters / filtering are extremely common, and it would be great if prisma supported such features.

@Weakky Weakky added kind/feature A request for a new feature. team/client Issue for team Client. topic: _count https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count- topic: filteredRelationCount labels Apr 26, 2024
@janpio
Copy link
Member

janpio commented Apr 29, 2024

Thanks for your issue @OllieJennings. Is this a duplicate of #8935?

@OllieJennings
Copy link
Author

@janpio yes it is! apologies tried searching and couldn't find the issue you just linked, happy to close this as this is effectively a dupe

@janpio
Copy link
Member

janpio commented Apr 30, 2024

All good, I understand how it can be a lot of issues to go or search through. Closing this now.

Make sure to 👍 the other issue and also subscribe to it via the right hand sidebar so you are notified of updated. Thanks!

@janpio janpio closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. topic: _count https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count- topic: filteredRelationCount topic: relations
Projects
None yet
Development

No branches or pull requests

3 participants