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

Method unique

lib/sqlalchemy/engine/result.py:1093–1131  ·  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

1091
1092 @_generative
1093 def unique(self, strategy: Optional[_UniqueFilterType] = None) -> Self:
1094 """Apply unique filtering to the objects returned by this
1095 :class:`_engine.Result`.
1096
1097 When this filter is applied with no arguments, the rows or objects
1098 returned will filtered such that each row is returned uniquely. The
1099 algorithm used to determine this uniqueness is by default the Python
1100 hashing identity of the whole tuple. In some cases a specialized
1101 per-entity hashing scheme may be used, such as when using the ORM, a
1102 scheme is applied which works against the primary key identity of
1103 returned objects.
1104
1105 The unique filter is applied **after all other filters**, which means
1106 if the columns returned have been refined using a method such as the
1107 :meth:`_engine.Result.columns` or :meth:`_engine.Result.scalars`
1108 method, the uniquing is applied to **only the column or columns
1109 returned**. This occurs regardless of the order in which these
1110 methods have been called upon the :class:`_engine.Result` object.
1111
1112 The unique filter also changes the calculus used for methods like
1113 :meth:`_engine.Result.fetchmany` and :meth:`_engine.Result.partitions`.
1114 When using :meth:`_engine.Result.unique`, these methods will continue
1115 to yield the number of rows or objects requested, after uniquing
1116 has been applied. However, this necessarily impacts the buffering
1117 behavior of the underlying cursor or datasource, such that multiple
1118 underlying calls to ``cursor.fetchmany()`` may be necessary in order
1119 to accumulate enough objects in order to provide a unique collection
1120 of the requested size.
1121
1122 :param strategy: a callable that will be applied to rows or objects
1123 being iterated, which should return an object that represents the
1124 unique value of the row. A Python ``set()`` is used to store
1125 these identities. If not passed, a default uniqueness strategy
1126 is used which may have been assembled by the source of this
1127 :class:`_engine.Result` object.
1128
1129 """
1130 self._unique_filter_state = (set(), strategy)
1131 return self
1132
1133 def columns(self, *col_expressions: _KeyIndexType) -> Self:
1134 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

Calls

no outgoing calls