MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / distinct

Method distinct

lib/sqlalchemy/orm/query.py:2677–2724  ·  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

2675 @_generative
2676 @_assertions(_no_statement_condition)
2677 def distinct(self, *expr: _ColumnExpressionArgument[Any]) -> Self:
2678 r"""Apply a ``DISTINCT`` to the query and return the newly resulting
2679 ``Query``.
2680
2681
2682 .. note::
2683
2684 The ORM-level :meth:`.distinct` call includes logic that will
2685 automatically add columns from the ORDER BY of the query to the
2686 columns clause of the SELECT statement, to satisfy the common need
2687 of the database backend that ORDER BY columns be part of the SELECT
2688 list when DISTINCT is used. These columns *are not* added to the
2689 list of columns actually fetched by the :class:`_query.Query`,
2690 however,
2691 so would not affect results. The columns are passed through when
2692 using the :attr:`_query.Query.statement` accessor, however.
2693
2694 .. deprecated:: 2.0 This logic is deprecated and will be removed
2695 in SQLAlchemy 2.0. See :ref:`migration_20_query_distinct`
2696 for a description of this use case in 2.0.
2697
2698 .. seealso::
2699
2700 :meth:`_sql.Select.distinct` - v2 equivalent method.
2701
2702 :param \*expr: optional column expressions. When present,
2703 the PostgreSQL dialect will render a ``DISTINCT ON (<expressions>)``
2704 construct.
2705
2706 .. deprecated:: 2.1 Passing expressions to
2707 :meth:`_orm.Query.distinct` is deprecated, use
2708 :func:`_postgresql.distinct_on` instead.
2709
2710 """
2711 if expr:
2712 warn_deprecated(
2713 "Passing expression to ``distinct`` to generate a DISTINCT "
2714 "ON clause is deprecated. Use instead the "
2715 "``postgresql.distinct_on`` function as an extension.",
2716 "2.1",
2717 )
2718 self._distinct = True
2719 self._distinct_on = self._distinct_on + tuple(
2720 coercions.expect(roles.ByOfRole, e) for e in expr
2721 )
2722 else:
2723 self._distinct = True
2724 return self
2725
2726 @_generative
2727 def ext(self, extension: SyntaxExtension) -> Self:

Calls 1

warn_deprecatedFunction · 0.85