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.

Filter based on number / count of relations

See original GitHub issue

Problem

I have an e-commerce platform that requires me to list cities if there is an associated product with that city. However, we have a couple cities that there is currently no products associated with and therefore we don’t want those cities to show up.

model Locations {
  id                       Int        @default(autoincrement()) @id
  country                  String
  location                 String     @unique
}

model Products {
  id               Int           @default(autoincrement()) @id
  title            String
  city             String
  base_price       Int
  hidden           Boolean       @default(false)
  locations        locations     @relation(fields: [city], references: [location])
}

Assuming I have 3 cities: Boston, New York City, Miami.

Boston and New York City both have products. However, Miami does not have a product associated with that location.

On my prisma.Locations.findMany({}), I only want to get Boston and New York City since Miami does not have any products associated with it.

How can I go about this?

Suggested solution

    const paths = await prisma.locations.findMany({
      where: {
        Products: {
          not: null // anything to show product relations greater than 0
        }
      }
    })

Additionally

Additionally, another feature would be to not only check if there is a relation but filter it based on relations that I don’t want appearing. For example if Miami in this case did have 1 product. But the product appeared as hidden: true. I would not want Miami to appear under prisma.locations.findMany() because the only product that it has is considered hidden: true.

Alternatives

Additional context

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

25reactions
alimehasincommented, Oct 2, 2021

It would be great if we can filter the users based on a min and a max number of posts (Get all users that has posts between 3 and 10)

14reactions
viperfxcommented, Mar 9, 2021

any reason why this feature request was closed? Filtering or ordering on counts of related fields will be really useful

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filter based on number / count of relations #3821 - GitHub
Problem I have an e-commerce platform that requires me to list cities if there is an associated product with that city.
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 >
laravel - getting the count of a relationship and filtering based ...
The model Project has many bids, I want to get the count of these bids and filter projects based on that using whereBetween....
Read more >
Aggregation, grouping, and summarizing (Concepts) - Prisma
Prisma Client allows you to count records, aggregate number fields, and select distinct ... The primary use case for having is to filter...
Read more >
Relations & rollups – Notion Help Center
Notion's relation property is designed to help you express useful ... Count empty : Counts the number of related pages with an empty...
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 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