Support aggregations (e.g. count) on nested relations
See original GitHub issueProblem
Right now if you want to just get the count of the model in the same query, it is not possible.
Suggested solution
Introduce an aggregate key in include/select for nested relation in addition to the where
key
const users = await prisma.user.findMany({
include: {
_count: {
select: {
posts: true,
},
},
},
})
Alternatives
Doing a separate aggregate call for fetching the count.
Additional context
Suggestion by @kitze from the livestreams.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:180
- Comments:17 (7 by maintainers)
Top Results From Across the Web
Aggregation, grouping, and summarizing (Concepts) - Prisma
Prisma Client allows you to count records, aggregate number fields, and select distinct field values. Aggregate. Prisma Client allows you to aggregate on ......
Read more >Nested aggregation | Elasticsearch Guide [8.5] | Elastic
A special single bucket aggregation that enables aggregating nested documents. For example, lets say we have an index of products, and each product...
Read more >How can I get ElasticSearch aggregations to count the parent ...
Use reverse nested aggregation. This will then create an aggregation with the nested counts and a sub aggregation with the parent counts.
Read more >Aggregations in Power Pivot - Microsoft Support
The decision of how to group the data is driven by the business question. For example, aggregations can answer the following questions: Counts...
Read more >Practical planning and execution of groupjoin and nested ...
Here, nested the query calculates a COUNT(*) over the inner table, ... empty to support the semantics of static (whole table) aggregation.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Does anyone have an alternative for getting the “user” with the highest number of “posts”?
I have been trying to wrap my head around the current
aggregate
preview api, but it doesn’t seem possible to get the record with the highest count of related records. I am guessing it is not currently possible in prisma (in a single prisma statement)?This would be very useful. Any update on this? Is this being worked on or planned to be worked on?