diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-12-27 12:39:15 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2018-12-27 12:39:15 -0500 |
| commit | ec5c9ebe6e7b8822de0c7bd19aea11ea2a582e29 (patch) | |
| tree | 30e11b50d9658d24b5e9dce8b12dce72a5cb7c5d | |
| parent | eae62d00049a24c028ba95e13b5199cfa59a1180 (diff) | |
| download | sqlalchemy-ec5c9ebe6e7b8822de0c7bd19aea11ea2a582e29.tar.gz | |
Implement SynonymProperty.get_history()
Implemented the ``.get_history()`` method, which also implies availability
of :attr:`.AttributeState.history`, for :func:`.synonym` attributes.
Previously, trying to access attribute history via a synonym would raise an
``AttributeError``.
Fixes: #3777
Change-Id: I20810a8b1a1bf630dbcb6622193c13cf4236b94a
| -rw-r--r-- | doc/build/changelog/unreleased_13/3777.rst | 8 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/descriptor_props.py | 4 | ||||
| -rw-r--r-- | test/orm/test_mapper.py | 17 |
3 files changed, 29 insertions, 0 deletions
diff --git a/doc/build/changelog/unreleased_13/3777.rst b/doc/build/changelog/unreleased_13/3777.rst new file mode 100644 index 000000000..043cdbf3f --- /dev/null +++ b/doc/build/changelog/unreleased_13/3777.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, orm + :tickets: 3777 + + Implemented the ``.get_history()`` method, which also implies availability + of :attr:`.AttributeState.history`, for :func:`.synonym` attributes. + Previously, trying to access attribute history via a synonym would raise an + ``AttributeError``. diff --git a/lib/sqlalchemy/orm/descriptor_props.py b/lib/sqlalchemy/orm/descriptor_props.py index 3fefc5bba..24a12193b 100644 --- a/lib/sqlalchemy/orm/descriptor_props.py +++ b/lib/sqlalchemy/orm/descriptor_props.py @@ -660,6 +660,10 @@ class SynonymProperty(DescriptorProperty): comp = prop.comparator_factory(prop, mapper) return comp + def get_history(self, *arg, **kw): + attr = getattr(self.parent.class_, self.name) + return attr.impl.get_history(*arg, **kw) + def set_parent(self, parent, init): if self.map_column: # implement the 'map_column' option. diff --git a/test/orm/test_mapper.py b/test/orm/test_mapper.py index 0ff9c12ad..487299f29 100644 --- a/test/orm/test_mapper.py +++ b/test/orm/test_mapper.py @@ -1381,6 +1381,23 @@ class MapperTest(_fixtures.FixtureTest, AssertsCompiledSQL): u = s.query(User).filter(User.y == 8).one() eq_(u.y, 8) + def test_synonym_get_history(self): + users, User = (self.tables.users, + self.classes.User) + + mapper(User, users, properties={ + 'x': synonym('id'), + 'y': synonym('x') + }) + + u1 = User() + eq_(attributes.instance_state(u1).attrs.x.history, (None, None, None)) + eq_(attributes.instance_state(u1).attrs.y.history, (None, None, None)) + + u1.y = 5 + eq_(attributes.instance_state(u1).attrs.x.history, ([5], (), ())) + eq_(attributes.instance_state(u1).attrs.y.history, ([5], (), ())) + def test_synonym_of_non_property_raises(self): from sqlalchemy.ext.associationproxy import association_proxy |
