diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2021-04-07 16:06:20 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2021-04-07 16:06:20 +0000 |
| commit | 57e46cde849d3d35e4e97e953ce29ee8bcc138a7 (patch) | |
| tree | bb1cffcefb344368759c0c4cde6ca37e03d79cb1 /lib/sqlalchemy | |
| parent | 4d21920638af4729b6ff09b1ac8c3a20878bd922 (diff) | |
| parent | 475a1ada5d3ac88f12080ef8672a8fda70c7e76e (diff) | |
| download | sqlalchemy-57e46cde849d3d35e4e97e953ce29ee8bcc138a7.tar.gz | |
Merge "Check for hybrid's attribute name and support no name"
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/ext/hybrid.py | 41 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/attributes.py | 18 |
2 files changed, 55 insertions, 4 deletions
diff --git a/lib/sqlalchemy/ext/hybrid.py b/lib/sqlalchemy/ext/hybrid.py index 5fcb4fac0..378e6c18b 100644 --- a/lib/sqlalchemy/ext/hybrid.py +++ b/lib/sqlalchemy/ext/hybrid.py @@ -263,6 +263,34 @@ is supported, for more complex SET expressions it will usually be necessary to use either the "fetch" or False synchronization strategy as illustrated above. +.. note:: For ORM bulk updates to work with hybrids, the function name + of the hybrid must match that of how it is accessed. Something + like this wouldn't work:: + + class Interval(object): + # ... + + def _get(self): + return self.end - self.start + + def _set(self, value): + self.end = self.start + value + + def _update_expr(cls, value): + return [ + (cls.end, cls.start + value) + ] + + length = hybrid_property( + fget=_get, fset=_set, update_expr=_update_expr + ) + + The Python descriptor protocol does not provide any reliable way for + a descriptor to know what attribute name it was accessed as, and + the UPDATE scheme currently relies upon being able to access the + attribute from an instance by name in order to perform the instance + synchronization step. + .. versionadded:: 1.2 added support for bulk updates to hybrid properties. Working with Relationships @@ -1098,9 +1126,20 @@ class hybrid_property(interfaces.InspectionAttrInfo): proxy_attr = attributes.create_proxied_attribute(self) def expr_comparator(owner): + # because this is the descriptor protocol, we don't really know + # what our attribute name is. so search for it through the + # MRO. + for lookup in owner.__mro__: + if self.__name__ in lookup.__dict__: + if lookup.__dict__[self.__name__] is self: + name = self.__name__ + break + else: + name = attributes.NO_KEY + return proxy_attr( owner, - self.__name__, + name, self, comparator(owner), doc=comparator.__doc__ or self.__doc__, 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): |
