Filter based on number / count of relations
See original GitHub issueProblem
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:
- Created 3 years ago
- Reactions:13
- Comments:11 (5 by maintainers)
Top GitHub Comments
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)
any reason why this feature request was closed? Filtering or ordering on counts of related fields will be really useful