ORM-specific compiler plugins receive the
:class:`_sql.Select` construct and interpret its contents in terms of an
ORM-style query, before passing off to the core-level compiler in order to
-create a SQL string. With the advent of the new `SQL compilation caching
-system <change_4639>`, the majority of this ORM logic is also cached.
+create a SQL string. With the advent of the new
+`SQL compilation caching system <change_4639>`,
+the majority of this ORM logic is also cached.
.. seealso::
subquery = session.query(User).filter(User.id == 5).subquery()
- ua = aliased(user, subquery)
+ ua = aliased(User, subquery)
user = session.query(ua).first()
subquery = select(User).where(User.id == 5).subquery()
- ua = aliased(user, subquery)
+ ua = aliased(User, subquery)
user = session.execute(select(ua)).scalars().first()
# statement will raise if unique() is not used, due to joinedload()
# of a collection. in all other cases, unique() is not needed
- rows = session.invoke(stmt).unique().execute().all()
+ rows = session.execute(stmt).unique().execute().all()
**Discussion**
return uniques, strategy
-class Result(ResultInternal):
+class _WithKeys(object):
+ # used mainly to share documentation on the keys method.
+ # py2k does not allow overriding the __doc__ attribute.
+ def keys(self):
+ """Return an iterable view which yields the string keys that would
+ be represented by each :class:`.Row`.
+
+ The keys can represent the labels of the columns returned by a core
+ statement or the names of the orm classes returned by an orm
+ execution.
+
+ The view also can be tested for key containment using the Python
+ ``in`` operator, which will test both for the string keys represented
+ in the view, as well as for alternate keys such as column objects.
+
+ .. versionchanged:: 1.4 a key view object is returned rather than a
+ plain list.
+
+
+ """
+ return self._metadata.keys
+
+
+class Result(_WithKeys, ResultInternal):
"""Represent a set of database results.
.. versionadded:: 1.4 The :class:`.Result` object provides a completely
def _soft_close(self, hard=False):
raise NotImplementedError()
- def keys(self):
- """Return an iterable view which yields the string keys that would
- be represented by each :class:`.Row`.
-
- The view also can be tested for key containment using the Python
- ``in`` operator, which will test both for the string keys represented
- in the view, as well as for alternate keys such as column objects.
-
- .. versionchanged:: 1.4 a key view object is returned rather than a
- plain list.
-
-
- """
- return self._metadata.keys
-
@_generative
def yield_per(self, num):
"""Configure the row-fetching strategy to fetch num rows at a time.
:paramref:`.Connection.execution_options.stream_results` execution
option is used indicating that the driver should not pre-buffer
results, if possible. Not all drivers support this option and
- the option is silently ignored for those who do.
+ the option is silently ignored for those who do not.
.. versionadded:: 1.4
return self._only_one_row(True, True, False)
-class MappingResult(FilterResult):
+class MappingResult(_WithKeys, FilterResult):
"""A wrapper for a :class:`_engine.Result` that returns dictionary values
rather than :class:`_engine.Row` values.
if result._source_supports_scalars:
self._metadata = self._metadata._reduce([0])
- def keys(self):
- """Return an iterable view which yields the string keys that would
- be represented by each :class:`.Row`.
-
- The view also can be tested for key containment using the Python
- ``in`` operator, which will test both for the string keys represented
- in the view, as well as for alternate keys such as column objects.
-
- .. versionchanged:: 1.4 a key view object is returned rather than a
- plain list.
-
-
- """
- return self._metadata.keys
-
def unique(self, strategy=None):
# type: () -> MappingResult
"""Apply unique filtering to the objects returned by this
"""Return the list of keys as strings represented by this
:class:`.Row`.
+ The keys can represent the labels of the columns returned by a core
+ statement or the names of the orm classes returned by an orm
+ execution.
+
This method is analogous to the Python dictionary ``.keys()`` method,
except that it returns a list, not an iterator.
"""Return a tuple of string keys as represented by this
:class:`.Row`.
+ The keys can represent the labels of the columns returned by a core
+ statement or the names of the orm classes returned by an orm
+ execution.
+
This attribute is analogous to the Python named tuple ``._fields``
attribute.