diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-04-07 10:26:31 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-04-07 10:26:31 -0400 |
commit | 475a1ada5d3ac88f12080ef8672a8fda70c7e76e (patch) | |
tree | 9279db69a507c329530f0ba227a88a9c0308ff9c /lib/sqlalchemy/orm/attributes.py | |
parent | d1847bbe268d48d34f4781b317ac8e6bf13d44bd (diff) | |
download | sqlalchemy-475a1ada5d3ac88f12080ef8672a8fda70c7e76e.tar.gz |
Check for hybrid's attribute name and support no name
Fixed regression where the ORM compilation scheme would assume the function
name of a hybrid property would be the same as the attribute name in such a
way that an ``AttributeError`` would be raised, when it would attempt to
determine the correct name for each element in a result tuple. A similar
issue exists in 1.3 but only impacts the names of tuple rows. The fix here
adds a check that the hybrid's function name is actually present in the
``__dict__`` of the class or its superclasses before assigning this name;
otherwise, the hybrid is considered to be "unnamed" and ORM result tuples
will use the naming scheme of the underlying expression.
Fixes: #6215
Change-Id: I584c0c05efec957f4dcaccf5df371399a57dffe9
Diffstat (limited to 'lib/sqlalchemy/orm/attributes.py')
-rw-r--r-- | lib/sqlalchemy/orm/attributes.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py index 610ee2726..d1ed17f1a 100644 --- a/lib/sqlalchemy/orm/attributes.py +++ b/lib/sqlalchemy/orm/attributes.py @@ -54,6 +54,13 @@ from ..sql import traversals from ..sql import visitors +class NoKey(str): + pass + + +NO_KEY = NoKey("no name") + + @inspection._self_inspects class QueryableAttribute( interfaces._MappedAttribute, @@ -214,10 +221,15 @@ class QueryableAttribute( subclass representing a column expression. """ + if self.key is NO_KEY: + annotations = {"entity_namespace": self._entity_namespace} + else: + annotations = { + "proxy_key": self.key, + "entity_namespace": self._entity_namespace, + } - return self.comparator.__clause_element__()._annotate( - {"proxy_key": self.key, "entity_namespace": self._entity_namespace} - ) + return self.comparator.__clause_element__()._annotate(annotations) @property def _entity_namespace(self): |