feat: add DISTINCT ON support for PostgreSQL.#2211
Open
VladislavYar wants to merge 2 commits into
Open
Conversation
waketzheng
reviewed
Jun 6, 2026
| return decorator | ||
|
|
||
|
|
||
| def skipCapability( |
Contributor
There was a problem hiding this comment.
So many lines of this function. I would prefer:
def skip_dialect(*names: str, connection_name: str = 'models') -> Callable[[_FT], _FT]:
return requireCapability(connection_name, dialect=NotIn(*names))
| await Tournament.all().distinct("name").order_by("desc") | ||
|
|
||
|
|
||
| @test.skipCapability(dialect="postgres") |
Contributor
There was a problem hiding this comment.
How about @test.requireCapability(dialect=NotIn("postgres"))
Author
There was a problem hiding this comment.
You're right. I removed the skipCapability function and changed it to requireCapability(dialect=NotIn("postgres")).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Added
DISTINCT ON (fields)support for PostgreSQL toQuerySet.distinct(*fields).Previously,
.distinct()only supported plainDISTINCT(no arguments). Now, passing fieldnames generates
DISTINCT ON (fields)on PostgreSQL, keeping one row per unique combinationof the specified fields. The feature is fully propagated to
.values(),.values_list(),and
.only().Also added:
skipCapabilitydecorator totortoise.contrib.test— the inverse ofrequireCapability,skips a test when the specified capabilities match.
_apply_db()helper in_ChooseDBMixinthat consistently sets the DB connection andswitches the query builder to
PostgreSQLQueryBuilderacross all query classes.Motivation and Context
PostgreSQL's
DISTINCT ONis a powerful feature that standardDISTINCTcannot replace —it allows selecting one representative row per group without a
GROUP BY, while retainingfull model objects. This is commonly needed for "latest per group" or "first per category"
queries.
How Has This Been Tested?
Added
tests/test_distinct.pycovering:DISTINCT ONwith and withoutORDER BYDISTINCT ONfields.values_list()— single field, multiple fields, field outsideDISTINCT ON.values()— same variations.only()ORDER BYrespects the selected row within each groupOperationalErrorwhenORDER BYdoesn't start withDISTINCT ONfieldsOperationalErrorwhen used on a non-PostgreSQL databaseChecklist: