tortoise / tortoise/tortoise-orm

Aggregations on explicit GROUP BY queries

Open
#329 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement Waiting for feedback
Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.
Currently, it is not possible to group entries by a specific field to apply aggregations. A raw SQL example would be:

SELECT AVG(gpa)
FROM students
WHERE age > 21
GROUP BY gender

Describe the solution you'd like
To keep the API similar to Django's, it would be nice to be able to use annotate() on ValuesQuery:

Student.filter(age__gt=21).values('gender').annotate(average_gpa=Avg('gpa'))

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the ValuesQuery API and its existing support for annotate(), using the Django aggregation example and the Student.filter(age__gt=21).values('gender').annotate(average_gpa=Avg('gpa')) expression as the target behavior. Done means explicit grouping can be combined with aggregation and produces the expected grouped results.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.