From 4f2d0913fe4fe4f5182f85903a6b3be65ac4fd94 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 19 Apr 2018 17:07:32 -0400 Subject: Ensure select_from_entity adapter is used in adjust_for_single_inheritance Fixed issue in single-inheritance loading where the use of an aliased entity against a single-inheritance subclass in conjunction with the :meth:`.Query.select_from` method would cause the SQL to be rendered with the unaliased table mixed in to the query, causing a cartesian product. In particular this was affecting the new "selectin" loader when used against a single-inheritance subclass. Change-Id: Ic2cbe94a5269c101b1f98da9a466180dd4452783 Fixes: #4241 --- lib/sqlalchemy/orm/query.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 54be93055..42f1b2673 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -3542,12 +3542,14 @@ class Query(object): """ search = set(self._mapper_adapter_map.values()) - if self._select_from_entity: - # based on the behavior in _set_select_from, - # when we have self._select_from_entity, we don't - # have _from_obj_alias. - # assert self._from_obj_alias is None - search = search.union([(self._select_from_entity, None)]) + if self._select_from_entity and \ + self._select_from_entity not in self._mapper_adapter_map: + insp = inspect(self._select_from_entity) + if insp.is_aliased_class: + adapter = insp._adapter + else: + adapter = None + search = search.union([(self._select_from_entity, adapter)]) for (ext_info, adapter) in search: if ext_info in self._join_entities: -- cgit v1.2.1