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

Method distinct

lib/sqlalchemy/sql/selectable.py:6525–6584  ·  view source on GitHub ↗

r"""Return a new :func:`_expression.select` construct which will apply DISTINCT to the SELECT statement overall. E.g.:: from sqlalchemy import select stmt = select(users_table.c.id, users_table.c.name).distinct() The above would produce an statemen

(self, *expr: _ColumnExpressionArgument[Any])

Source from the content-addressed store, hash-verified

6523
6524 @_generative
6525 def distinct(self, *expr: _ColumnExpressionArgument[Any]) -> Self:
6526 r"""Return a new :func:`_expression.select` construct which
6527 will apply DISTINCT to the SELECT statement overall.
6528
6529 E.g.::
6530
6531 from sqlalchemy import select
6532
6533 stmt = select(users_table.c.id, users_table.c.name).distinct()
6534
6535 The above would produce an statement resembling:
6536
6537 .. sourcecode:: sql
6538
6539 SELECT DISTINCT user.id, user.name FROM user
6540
6541 The method also historically accepted an ``*expr`` parameter which
6542 produced the PostgreSQL dialect-specific ``DISTINCT ON`` expression.
6543 This is now replaced using the :func:`_postgresql.distinct_on`
6544 extension::
6545
6546 from sqlalchemy import select
6547 from sqlalchemy.dialects.postgresql import distinct_on
6548
6549 stmt = select(users_table).ext(distinct_on(users_table.c.name))
6550
6551 Using this parameter on other backends which don't support this
6552 syntax will raise an error.
6553
6554 :param \*expr: optional column expressions. When present,
6555 the PostgreSQL dialect will render a ``DISTINCT ON (<expressions>)``
6556 construct. A deprecation warning and/or :class:`_exc.CompileError`
6557 will be raised on other backends.
6558
6559 .. deprecated:: 2.1 Passing expressions to
6560 :meth:`_sql.Select.distinct` is deprecated, use
6561 :func:`_postgresql.distinct_on` instead.
6562
6563 .. deprecated:: 1.4 Using \*expr in other dialects is deprecated
6564 and will raise :class:`_exc.CompileError` in a future version.
6565
6566 .. seealso::
6567
6568 :func:`_postgresql.distinct_on`
6569
6570 :meth:`.ext`
6571 """
6572 self._distinct = True
6573 if expr:
6574 warn_deprecated(
6575 "Passing expression to ``distinct`` to generate a "
6576 "DISTINCT ON clause is deprecated. Use instead the "
6577 "``postgresql.distinct_on`` function as an extension.",
6578 "2.1",
6579 )
6580 self._distinct_on = self._distinct_on + tuple(
6581 coercions.expect(roles.ByOfRole, e, apply_propagate_attrs=self)
6582 for e in expr

Callers 1

distinctFunction · 0.45

Calls 1

warn_deprecatedFunction · 0.85

Tested by

no test coverage detected