tortoise / tortoise/tortoise-orm

How to achieve SELECT DISTINCT field .....

Open
#959 2 comments 1 reaction 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

Model
class Transactions(Model):
id = fields.CharField(65, pk=True, null=False)
confirmation = fields.IntField(pk=False, max_length=1, default=0, null=False)
confirmed_hash = fields.CharField(65, null=False)

Dataset:
id - confirmation - confirmed_hash
1 - 0 - aaaaaaaaaaaaaaaaaaaa
2 - 1 - aaaaaaaaaaaaaaaaaaaa
3 - 1 - bbbbbbbbbbbbbbbbbb
4 - 0 - aaaaaaaaaaaaaaaaaaaa
5 - 0 - cccccccccccccccccccc
6 - 0 - cccccccccccccccccccc
7 - 1 - aaaaaaaaaaaaaaaaaaaa

From this dataset I want to return only the DISTINCT 'confirmed_hash' rows.
I would expect to do this like this:

Transactions.all().distinct('confirmed_hash')

to produce SQL along the lines of

"SELECT DISTINCT confirmed_hash FROM Transactions....."

Which would return:

[
'aaaaaaaaaaaaaaaaaaaa',
bbbbbbbbbbbbbbbbbb',
'cccccccccccccccccccc'
]

Can anyone clarify how to achieve this with Tortoise?
Thanks

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

The issue names the Transactions model and the QuerySet distinct operation, but no source files or tests. Start by tracing the Tortoise query API for distinct field selection and compare its generated SQL with the requested confirmed_hash-only result. Done means the documented or implemented query returns one value for each distinct confirmed_hash.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
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.