MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / distinct

Method distinct

lib/sqlalchemy/orm/query.py:2647–2687  ·  view source on GitHub ↗

r"""Apply a ``DISTINCT`` to the query and return the newly resulting ``Query``. .. note:: The ORM-level :meth:`.distinct` call includes logic that will automatically add columns from the ORDER BY of the query to the columns clause of the SELECT

(self, *expr: _ColumnExpressionArgument[Any])

Source from the content-addressed store, hash-verified

2645 @_generative
2646 @_assertions(_no_statement_condition)
2647 def distinct(self, *expr: _ColumnExpressionArgument[Any]) -> Self:
2648 r"""Apply a ``DISTINCT`` to the query and return the newly resulting
2649 ``Query``.
2650
2651
2652 .. note::
2653
2654 The ORM-level :meth:`.distinct` call includes logic that will
2655 automatically add columns from the ORDER BY of the query to the
2656 columns clause of the SELECT statement, to satisfy the common need
2657 of the database backend that ORDER BY columns be part of the SELECT
2658 list when DISTINCT is used. These columns *are not* added to the
2659 list of columns actually fetched by the :class:`_query.Query`,
2660 however,
2661 so would not affect results. The columns are passed through when
2662 using the :attr:`_query.Query.statement` accessor, however.
2663
2664 .. deprecated:: 2.0 This logic is deprecated and will be removed
2665 in SQLAlchemy 2.0. See :ref:`migration_20_query_distinct`
2666 for a description of this use case in 2.0.
2667
2668 .. seealso::
2669
2670 :meth:`_sql.Select.distinct` - v2 equivalent method.
2671
2672 :param \*expr: optional column expressions. When present,
2673 the PostgreSQL dialect will render a ``DISTINCT ON (<expressions>)``
2674 construct.
2675
2676 .. deprecated:: 1.4 Using \*expr in other dialects is deprecated
2677 and will raise :class:`_exc.CompileError` in a future version.
2678
2679 """
2680 if expr:
2681 self._distinct = True
2682 self._distinct_on = self._distinct_on + tuple(
2683 coercions.expect(roles.ByOfRole, e) for e in expr
2684 )
2685 else:
2686 self._distinct = True
2687 return self
2688
2689 def all(self) -> List[_T]:
2690 """Return the results represented by this :class:`_query.Query`

Calls

no outgoing calls