tortoise / tortoise/tortoise-orm

How to achieve SELECT count(distinct step_id)...

Open
#1,149 0 comments 0 reactions 0 assignees View on GitHub

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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.