summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/query.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-01-28 16:44:53 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2020-01-29 22:02:34 -0500
commitc741b89bd57eb31b7a1bbd187a4d159bdfea5111 (patch)
tree011bce072c9093cc4b648c669212a58bbd9a3d57 /lib/sqlalchemy/orm/query.py
parent0cc2695510c0f0b09328e4cdf8d3ae29ac7f7abd (diff)
downloadsqlalchemy-c741b89bd57eb31b7a1bbd187a4d159bdfea5111.tar.gz
Raise for unexpected polymorphic identity
A query that is against an mapped inheritance subclass which also uses :meth:`.Query.select_entity_from` or a similar technique in order to provide an existing subquery to SELECT from, will now raise an error if the given subquery returns entities that do not correspond to the given subclass, that is, they are sibling or superclasses in the same hierarchy. Previously, these would be returned without error. Additionally, if the inheritance mapping is a single-inheritance mapping, the given subquery must apply the appropriate filtering against the polymorphic discriminator column in order to avoid this error; previously, the :class:`.Query` would add this criteria to the outside query however this interferes with some kinds of query that return other kinds of entities as well. Fixes: #5122 Change-Id: I60cf8c1300d5bb279ad99f0f01fefe7e008a159b
Diffstat (limited to 'lib/sqlalchemy/orm/query.py')
-rw-r--r--lib/sqlalchemy/orm/query.py8
1 files changed, 4 insertions, 4 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 55b569bcd..15319e049 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -260,6 +260,7 @@ class Query(Generative):
self._from_obj_alias = sql_util.ColumnAdapter(
self._from_obj[0], equivs
)
+ self._enable_single_crit = False
elif (
set_base_alias
and len(self._from_obj) == 1
@@ -267,6 +268,7 @@ class Query(Generative):
and info.is_aliased_class
):
self._from_obj_alias = info._adapter
+ self._enable_single_crit = False
def _reset_polymorphic_adapter(self, mapper):
for m2 in mapper._with_polymorphic_mappers:
@@ -1379,7 +1381,6 @@ class Query(Generative):
._anonymous_fromclause()
)
q = self._from_selectable(fromclause)
- q._enable_single_crit = False
q._select_from_entity = self._entity_zero()
if entities:
q._set_entities(entities)
@@ -1407,6 +1408,7 @@ class Query(Generative):
):
self.__dict__.pop(attr, None)
self._set_select_from([fromclause], True)
+ self._enable_single_crit = False
# this enables clause adaptation for non-ORM
# expressions.
@@ -1875,9 +1877,7 @@ class Query(Generative):
self._having = criterion
def _set_op(self, expr_fn, *q):
- return self._from_selectable(
- expr_fn(*([self] + list(q))).subquery()
- )._set_enable_single_crit(False)
+ return self._from_selectable(expr_fn(*([self] + list(q))).subquery())
def union(self, *q):
"""Produce a UNION of this Query against one or more queries.