tortoise / tortoise/tortoise-orm
How to achieve SELECT count(distinct step_id)...
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Describe the bug
When i use annotate to count with distinct a group by appears
To Reproduce
Models.playedStep.filter(session__scenario_id=idScenario).filter(session__user_id=idUser).annotate(count=Count('step_id',distinct=True)).values('count').sql()
SELECT COUNT(DISTINCT "playedSteps"."step_id") "count" FROM "playedSteps" LEFT OUTER JOIN "session" "playedSteps__session" ON "playedSteps__session"."id"="playedSteps"."session_id" WHERE "playedSteps__session"."scenario_id"=1 AND "playedSteps__session"."user_id"=56 GROUP BY "playedSteps"."id";
Expected behavior
select count(distinct step_id) from "playedSteps" inner join session s on s.id = "playedSteps".session_id where scenario_id=1;
Additional context
how can i achieve this request with tortoise ? ( no group by )
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ORM query path for annotate(), Count(..., distinct=True), values(), and SQL generation, using the reproduction as the entry point. Compare the generated GROUP BY with the expected count query and determine what behavior should be covered by a regression test. Done means the equivalent query can produce COUNT(DISTINCT step_id) without the unwanted GROUP BY.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100