diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-03-29 14:24:39 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-03-29 22:27:41 -0400 |
commit | a65d5c250e9fd7090311ef12f28d7d959c6c738e (patch) | |
tree | 349f0b4c1127c3d87d9cffb62b5d6e02979a3a9a /lib/sqlalchemy/orm/query.py | |
parent | 8e857e3f6beecf7510f741428d8d0ba24f5cb71b (diff) | |
download | sqlalchemy-a65d5c250e9fd7090311ef12f28d7d959c6c738e.tar.gz |
Add a third labeling mode for SELECT statements
Enhanced the disambiguating labels feature of the
:func:`~.sql.expression.select` construct such that when a select statement
is used in a subquery, repeated column names from different tables are now
automatically labeled with a unique label name, without the need to use the
full "apply_labels()" feature that conbines tablename plus column name.
The disambigated labels are available as plain string keys in the .c
collection of the subquery, and most importantly the feature allows an ORM
:func:`.orm.aliased` construct against the combination of an entity and an
arbitrary subquery to work correctly, targeting the correct columns despite
same-named columns in the source tables, without the need for an "apply
labels" warning.
The existing labeling style is now called
LABEL_STYLE_TABLENAME_PLUS_COL. This labeling style will remain used
throughout the ORM as has been the case for over a decade, however,
the new disambiguation scheme could theoretically replace this scheme
entirely. The new scheme would dramatically alter how SQL looks
when rendered from the ORM to be more succinct but arguably harder
to read.
The tablename_columnname scheme used by Join.c is unaffected here,
as that's still hardcoded to that scheme.
Fixes: #5221
Change-Id: Ib47d9e0f35046b3afc77bef6e65709b93d0c3026
Diffstat (limited to 'lib/sqlalchemy/orm/query.py')
-rw-r--r-- | lib/sqlalchemy/orm/query.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index de11ba7dc..4ba82d1e8 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -56,6 +56,7 @@ from ..sql.base import _generative from ..sql.base import ColumnCollection from ..sql.base import Generative from ..sql.selectable import ForUpdateArg +from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL from ..util import collections_abc __all__ = ["Query", "QueryContext", "aliased"] @@ -1209,6 +1210,14 @@ class Query(Generative): self.session = session + @util.deprecated_20( + ":meth:`.Query.from_self`", + alternative="The new approach is to use the :func:`.orm.aliased` " + "construct in conjunction with a subquery. See the section " + ":ref:`Selecting from the query itself as a subquery " + "<migration_20_query_from_self>` in the 2.0 migration notes for an " + "example.", + ) def from_self(self, *entities): r"""return a Query that selects from this Query's SELECT statement. @@ -3313,7 +3322,7 @@ class Query(Generative): def __iter__(self): context = self._compile_context() - context.statement.use_labels = True + context.statement.label_style = LABEL_STYLE_TABLENAME_PLUS_COL if self._autoflush and not self._populate_existing: self.session._autoflush() return self._execute_and_instances(context) |