diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-10-26 11:28:42 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-10-26 13:28:45 -0400 |
| commit | d2cf7dcfe0cd7e9986376b6e7edd4b7d60108599 (patch) | |
| tree | a3d1f5bd15525dbc49afdae135f7ecdd16e25866 /lib/sqlalchemy | |
| parent | 7cf3e79991b3d00d53bfb98cfdab267b67a5cdda (diff) | |
| download | sqlalchemy-d2cf7dcfe0cd7e9986376b6e7edd4b7d60108599.tar.gz | |
dont pull filter_by from from_obj, entity is hopefully sufficient
Fixed 1.4 regression where :meth:`_orm.Query.filter_by` would not function
correctly on a :class:`_orm.Query` that was produced from
:meth:`_orm.Query.union`, :meth:`_orm.Query.from_self` or similar.
Fixes: #7239
Change-Id: I3a0c3fd71180b1bfb6bf855f436a29c729664082
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/query.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index fcb9dae57..d48e34923 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -1707,12 +1707,42 @@ class Query( return None def _filter_by_zero(self): + """for the filter_by() method, return the target entity for which + we will attempt to derive an expression from based on string name. + + """ if self._legacy_setup_joins: _last_joined_entity = self._last_joined_entity if _last_joined_entity is not None: return _last_joined_entity - if self._from_obj: + # discussion related to #7239 + # special check determines if we should try to derive attributes + # for filter_by() from the "from object", i.e., if the user + # called query.select_from(some selectable).filter_by(some_attr=value). + # We don't want to do that in the case that methods like + # from_self(), select_entity_from(), or a set op like union() were + # called; while these methods also place a + # selectable in the _from_obj collection, they also set up + # the _set_base_alias boolean which turns on the whole "adapt the + # entity to this selectable" thing, meaning the query still continues + # to construct itself in terms of the lead entity that was passed + # to query(), e.g. query(User).from_self() is still in terms of User, + # and not the subquery that from_self() created. This feature of + # "implicitly adapt all occurrences of entity X to some arbitrary + # subquery" is the main thing I am trying to do away with in 2.0 as + # users should now used aliased() for that, but I can't entirely get + # rid of it due to query.union() and other set ops relying upon it. + # + # compare this to the base Select()._filter_by_zero() which can + # just return self._from_obj[0] if present, because there is no + # "_set_base_alias" feature. + # + # IOW, this conditional essentially detects if + # "select_from(some_selectable)" has been called, as opposed to + # "select_entity_from()", "from_self()" + # or "union() / some_set_op()". + if self._from_obj and not self._compile_options._set_base_alias: return self._from_obj[0] return self._raw_columns[0] |
