diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-03-05 16:02:38 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-03-05 16:06:26 -0500 |
| commit | 8b8a4c373e1cec2203f73b1772704b9ded75c1fe (patch) | |
| tree | a9129521118f2b4a54bd1c912c9e9497b47371b6 /lib/sqlalchemy | |
| parent | 2929d5a1e08e9bd1f12d5a1a059018bf92ef6e29 (diff) | |
| download | sqlalchemy-8b8a4c373e1cec2203f73b1772704b9ded75c1fe.tar.gz | |
Ensure all Query statements compile w/ orm, fix test harness
Fixed regression where :meth:`_orm.Query.join` would produce no effect if
the query itself as well as the join target were against a
:class:`_schema.Table` object, rather than a mapped class. This was part of
a more systemic issue where the legacy ORM query compiler would not be
correctly used from a :class:`_orm.Query` if the statement produced had not
ORM entities present within it.
Also repair the assert_compile() method, which was using
Query._compile_state() which was bypassing the bug. A handful
of ORM tests with Query objects and Core-only objects were actually
failing if the default compilation path were used.
Fixes: #6003
Change-Id: I97478b2d06bf6c01fe1f09ee118e1f2ac4c849bc
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/query.py | 10 | ||||
| -rw-r--r-- | lib/sqlalchemy/testing/assertions.py | 8 |
2 files changed, 13 insertions, 5 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index c444b557b..d685ac83e 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -440,6 +440,15 @@ class Query( ) stmt.__dict__.pop("session", None) + # ensure the ORM context is used to compile the statement, even + # if it has no ORM entities. This is so ORM-only things like + # _legacy_joins are picked up that wouldn't be picked up by the + # Core statement context + if "compile_state_plugin" not in stmt._propagate_attrs: + stmt._propagate_attrs = stmt._propagate_attrs.union( + {"compile_state_plugin": "orm", "plugin_subject": None} + ) + return stmt def subquery( @@ -2304,6 +2313,7 @@ class Query( "is deprecated and will be removed in SQLAlchemy 2.0. " "Please use individual join() calls per relationship." ) + self._legacy_setup_joins += joins_to_add self.__dict__.pop("_last_joined_entity", None) diff --git a/lib/sqlalchemy/testing/assertions.py b/lib/sqlalchemy/testing/assertions.py index 81c74e7c1..289ac9a0a 100644 --- a/lib/sqlalchemy/testing/assertions.py +++ b/lib/sqlalchemy/testing/assertions.py @@ -445,11 +445,9 @@ class AssertsCompiledSQL(object): from sqlalchemy import orm if isinstance(clause, orm.Query): - compile_state = clause._compile_state() - compile_state.statement._label_style = ( - LABEL_STYLE_TABLENAME_PLUS_COL - ) - clause = compile_state.statement + stmt = clause._statement_20() + stmt._label_style = LABEL_STYLE_TABLENAME_PLUS_COL + clause = stmt if compile_kwargs: kw["compile_kwargs"] = compile_kwargs |
