summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-11-16 10:15:17 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2020-11-16 10:45:07 -0500
commit218fc83f3997dc0c152278eea0740b088074784b (patch)
tree11d63c757b7edf00160a4043af0b8f0d9fb466cf /lib/sqlalchemy
parent44a656a24242e160d5da3b45f4f65d2b2471912a (diff)
downloadsqlalchemy-218fc83f3997dc0c152278eea0740b088074784b.tar.gz
Ensure the "orm" plugin is used unconditionally for bundles
This also introduces that the "orm" plugin may be used when the plugin_subject is None. Fixed regression where the :paramref:`.Bundle.single_entity` flag would take effect for a :class:`.Bundle` even though it were not set. Additionally, this flag is legacy as it only makes sense for the :class:`_orm.Query` object and not 2.0 style execution. a deprecation warning is emitted when used with new-style execution. Fixes: #5702 Change-Id: If9f43365b9d966cb398890aeba2efa555658a7e5
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/orm/context.py19
-rw-r--r--lib/sqlalchemy/orm/strategies.py9
-rw-r--r--lib/sqlalchemy/orm/util.py25
-rw-r--r--lib/sqlalchemy/sql/traversals.py4
4 files changed, 45 insertions, 12 deletions
diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py
index 12759f018..cd654ed3d 100644
--- a/lib/sqlalchemy/orm/context.py
+++ b/lib/sqlalchemy/orm/context.py
@@ -242,7 +242,8 @@ class ORMCompileState(CompileState):
except KeyError:
assert False, "statement had 'orm' plugin but no plugin_subject"
else:
- bind_arguments["mapper"] = plugin_subject.mapper
+ if plugin_subject:
+ bind_arguments["mapper"] = plugin_subject.mapper
if load_options._autoflush:
session._autoflush()
@@ -342,10 +343,10 @@ class ORMFromStatementCompileState(ORMCompileState):
self._polymorphic_adapters = {}
self._no_yield_pers = set()
- _QueryEntity.to_compile_state(self, statement_container._raw_columns)
-
self.compile_options = statement_container._compile_options
+ _QueryEntity.to_compile_state(self, statement_container._raw_columns)
+
self.current_path = statement_container._compile_options._current_path
if toplevel and statement_container._with_options:
@@ -494,10 +495,10 @@ class ORMSelectCompileState(ORMCompileState, SelectState):
)
self._setup_with_polymorphics()
- _QueryEntity.to_compile_state(self, select_statement._raw_columns)
-
self.compile_options = select_statement._compile_options
+ _QueryEntity.to_compile_state(self, select_statement._raw_columns)
+
# determine label style. we can make different decisions here.
# at the moment, trying to see if we can always use DISAMBIGUATE_ONLY
# rather than LABEL_STYLE_NONE, and if we can use disambiguate style
@@ -2365,6 +2366,14 @@ class _BundleEntity(_QueryEntity):
)
self.supports_single_entity = self.bundle.single_entity
+ if (
+ self.supports_single_entity
+ and not compile_state.compile_options._use_legacy_query_style
+ ):
+ util.warn_deprecated_20(
+ "The Bundle.single_entity flag has no effect when "
+ "using 2.0 style execution."
+ )
@property
def mapper(self):
diff --git a/lib/sqlalchemy/orm/strategies.py b/lib/sqlalchemy/orm/strategies.py
index 1d4709726..7f7bab682 100644
--- a/lib/sqlalchemy/orm/strategies.py
+++ b/lib/sqlalchemy/orm/strategies.py
@@ -2730,7 +2730,14 @@ class SelectInLoader(PostLoader, util.MemoizedSlots):
q = sql.lambda_stmt(
lambda: sql.select(
orm_util.Bundle("pk", *pk_cols), effective_entity
- ).apply_labels(),
+ )
+ .apply_labels()
+ ._set_propagate_attrs(
+ {
+ "compile_state_plugin": "orm",
+ "plugin_subject": effective_entity,
+ }
+ ),
lambda_cache=self._query_cache,
global_track_bound_values=False,
track_on=(self, effective_entity) + tuple(pk_cols),
diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py
index 7d1650a1a..4502d5b89 100644
--- a/lib/sqlalchemy/orm/util.py
+++ b/lib/sqlalchemy/orm/util.py
@@ -1361,11 +1361,26 @@ class Bundle(ORMColumnsClauseRole, SupportsCloneAnnotations, InspectionAttr):
# ensure existing entity_namespace remains
annotations = {"bundle": self, "entity_namespace": self}
annotations.update(self._annotations)
- return expression.ClauseList(
- _literal_as_text_role=roles.ColumnsClauseRole,
- group=False,
- *[e._annotations.get("bundle", e) for e in self.exprs]
- )._annotate(annotations)
+
+ plugin_subject = self.exprs[0]._propagate_attrs.get(
+ "plugin_subject", self.entity
+ )
+ return (
+ expression.ClauseList(
+ _literal_as_text_role=roles.ColumnsClauseRole,
+ group=False,
+ *[e._annotations.get("bundle", e) for e in self.exprs]
+ )
+ ._annotate(annotations)
+ ._set_propagate_attrs(
+ # the Bundle *must* use the orm plugin no matter what. the
+ # subject can be None but it's much better if it's not.
+ {
+ "compile_state_plugin": "orm",
+ "plugin_subject": plugin_subject,
+ }
+ )
+ )
@property
def clauses(self):
diff --git a/lib/sqlalchemy/sql/traversals.py b/lib/sqlalchemy/sql/traversals.py
index a24d010cd..2887813ad 100644
--- a/lib/sqlalchemy/sql/traversals.py
+++ b/lib/sqlalchemy/sql/traversals.py
@@ -176,7 +176,9 @@ class HasCacheKey(object):
obj["compile_state_plugin"],
obj["plugin_subject"]._gen_cache_key(
anon_map, bindparams
- ),
+ )
+ if obj["plugin_subject"]
+ else None,
)
elif meth is InternalTraversal.dp_annotations_key:
# obj is here is the _annotations dict. however, we