-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Search and filter by relation count, with eq, gt, lt etc. #8935
Copy link
Copy link
Open
Labels
kind/featureA request for a new feature.A request for a new feature.topic: _counthttps://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count-https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count-topic: relationstopic: where / filter
Description
Problem
I think the issue should be related with #3821
model Topic {
id Int @id @default(autoincrement())
title String @db.MediumText
content String @db.LongText
voters VotersOnTopics[]
watchers WatchersOnTopics[]
viewers ViewerOnTopics[]
}model VotersOnTopics {
voter User @relation(fields: [voterId], references: [id])
voterId Int
topic Topic @relation(fields: [topicId], references: [id])
topicId Int
votedAt DateTime @default(now())
@@id([voterId, topicId])
}model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
username String @unique
name String
email String @unique
avatarUrl String?
comments Comment[]
votedTopics VotersOnTopics[]
watchedTopics WatchersOnTopics[]
viewedTopics ViewerOnTopics[] @relation("viewed")
}The topic could be voted by many users
so what if I want to search by a votes condition ?
const result = await db.topic.findMany({
where: {
title: 'Hello world'
},
select: {
_count: {
select: {
watchers: true,
voters: true,
},
},
},
}
})There are no _count could be use in where conditions
Suggested solution
const result = await db.topic.findMany({
where: {
voters: {
_count: {
gt: 40,
lt: 100
}
}
},
select: {
_count: {
select: {
watchers: true,
voters: true,
},
},
},
}
})Alternatives
Additional context
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
kind/featureA request for a new feature.A request for a new feature.topic: _counthttps://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count-https://www.prisma.io/docs/concepts/components/prisma-client/aggregation-grouping-summarizing#count-topic: relationstopic: where / filter