MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / unique

Method unique

lib/sqlalchemy/engine/result.py:627–665  ·  view source on GitHub ↗

Apply unique filtering to the objects returned by this :class:`_engine.Result`. When this filter is applied with no arguments, the rows or objects returned will filtered such that each row is returned uniquely. The algorithm used to determine this uniqueness is by de

(self, strategy: Optional[_UniqueFilterType] = None)

Source from the content-addressed store, hash-verified

625
626 @_generative
627 def unique(self, strategy: Optional[_UniqueFilterType] = None) -> Self:
628 """Apply unique filtering to the objects returned by this
629 :class:`_engine.Result`.
630
631 When this filter is applied with no arguments, the rows or objects
632 returned will filtered such that each row is returned uniquely. The
633 algorithm used to determine this uniqueness is by default the Python
634 hashing identity of the whole tuple. In some cases a specialized
635 per-entity hashing scheme may be used, such as when using the ORM, a
636 scheme is applied which works against the primary key identity of
637 returned objects.
638
639 The unique filter is applied **after all other filters**, which means
640 if the columns returned have been refined using a method such as the
641 :meth:`_engine.Result.columns` or :meth:`_engine.Result.scalars`
642 method, the uniquing is applied to **only the column or columns
643 returned**. This occurs regardless of the order in which these
644 methods have been called upon the :class:`_engine.Result` object.
645
646 The unique filter also changes the calculus used for methods like
647 :meth:`_engine.Result.fetchmany` and :meth:`_engine.Result.partitions`.
648 When using :meth:`_engine.Result.unique`, these methods will continue
649 to yield the number of rows or objects requested, after uniquing
650 has been applied. However, this necessarily impacts the buffering
651 behavior of the underlying cursor or datasource, such that multiple
652 underlying calls to ``cursor.fetchmany()`` may be necessary in order
653 to accumulate enough objects in order to provide a unique collection
654 of the requested size.
655
656 :param strategy: a callable that will be applied to rows or objects
657 being iterated, which should return an object that represents the
658 unique value of the row. A Python ``set()`` is used to store
659 these identities. If not passed, a default uniqueness strategy
660 is used which may have been assembled by the source of this
661 :class:`_engine.Result` object.
662
663 """
664 self._unique_filter_state = (set(), strategy)
665 return self
666
667 def columns(self, *col_expressions: _KeyIndexType) -> Self:
668 r"""Establish the columns that should be returned in each row.

Callers 15

_load_on_pk_identityFunction · 0.45
do_loadFunction · 0.45
_emit_lazyloadMethod · 0.45
_load_via_childMethod · 0.45
_load_via_parentMethod · 0.45
_iterMethod · 0.45
instancesMethod · 0.45
test_generative_casesMethod · 0.45
go_all_uqMethod · 0.45
go_iter_uqMethod · 0.45

Calls

no outgoing calls