diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-05-01 08:41:09 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-05-01 11:54:21 -0400 |
commit | 5cde12d816b8b32719cee377677667150df07c48 (patch) | |
tree | 2bfe72c72617f617d80301d08e19a845211fad9f /lib/sqlalchemy/sql/base.py | |
parent | a47c158a9a3b1104698fc0bff47ca58d67cb9191 (diff) | |
download | sqlalchemy-5cde12d816b8b32719cee377677667150df07c48.tar.gz |
Support filter_by() from columns, functions, Core SQL
Fixed regression where :meth:`_orm.Query.filter_by` would not work if the
lead entity were a SQL function or other expression derived from the
primary entity in question, rather than a simple entity or column of that
entity. Additionally, improved the behavior of
:meth:`_sql.Select.filter_by` overall to work with column expressions even
in a non-ORM context.
Fixes: #6414
Change-Id: I316b5bf98293bec1ede08787f6181dd14be85419
Diffstat (limited to 'lib/sqlalchemy/sql/base.py')
-rw-r--r-- | lib/sqlalchemy/sql/base.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index d9f05e823..6d65d9061 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -15,6 +15,7 @@ import operator import re from . import roles +from . import visitors from .traversals import HasCacheKey # noqa from .traversals import HasCopyInternals # noqa from .traversals import MemoizedHasCacheKey # noqa @@ -1575,6 +1576,23 @@ def _bind_or_error(schemaitem, msg=None): return bind +def _entity_namespace(entity): + """Return the nearest .entity_namespace for the given entity. + + If not immediately available, does an iterate to find a sub-element + that has one, if any. + + """ + try: + return entity.entity_namespace + except AttributeError: + for elem in visitors.iterate(entity): + if hasattr(elem, "entity_namespace"): + return elem.entity_namespace + else: + raise + + def _entity_namespace_key(entity, key): """Return an entry from an entity_namespace. @@ -1584,8 +1602,8 @@ def _entity_namespace_key(entity, key): """ - ns = entity.entity_namespace try: + ns = _entity_namespace(entity) return getattr(ns, key) except AttributeError as err: util.raise_( |