From: Jeong YunWon Date: Fri, 27 Jan 2017 14:29:59 +0000 (+0900) Subject: Fix nested index_property setter when there is no container value X-Git-Tag: rel_1_1_6~19 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f411cac35001e0d40a6217846d3df40f676a2d4d;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix nested index_property setter when there is no container value Fixed bug in new :mod:`sqlalchemy.ext.indexable` extension where setting of a property that itself refers to another property would fail. Fixes: #3901 Change-Id: I203a66117e2399afee11a34f43f0e93adfc6d571 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index 906f570ff8..72948aaaef 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,14 @@ .. changelog:: :version: 1.1.6 + .. change:: 3901 + :tags: bug, ext + :tickets: 3901 + + Fixed bug in new :mod:`sqlalchemy.ext.indexable` extension + where setting of a property that itself refers to another property + would fail. + .. change:: 3900 :tags: bug, postgresql :tickets: 3900 diff --git a/lib/sqlalchemy/ext/indexable.py b/lib/sqlalchemy/ext/indexable.py index 8298a65a9a..b1ce129231 100644 --- a/lib/sqlalchemy/ext/indexable.py +++ b/lib/sqlalchemy/ext/indexable.py @@ -227,6 +227,7 @@ The above query will render:: """ from __future__ import absolute_import +from sqlalchemy import inspect from ..orm.attributes import flag_modified from ..ext.hybrid import hybrid_property @@ -318,13 +319,14 @@ class index_property(hybrid_property): # noqa def fset(self, instance, value): attr_name = self.attr_name - column_value = getattr(instance, attr_name) + column_value = getattr(instance, attr_name, None) if column_value is None: column_value = self.datatype() setattr(instance, attr_name, column_value) column_value[self.index] = value setattr(instance, attr_name, column_value) - flag_modified(instance, attr_name) + if attr_name in inspect(instance).mapper.attrs: + flag_modified(instance, attr_name) def fdel(self, instance): attr_name = self.attr_name diff --git a/test/ext/test_indexable.py b/test/ext/test_indexable.py index 3df49cf86a..76a99aa6dc 100644 --- a/test/ext/test_indexable.py +++ b/test/ext/test_indexable.py @@ -372,3 +372,17 @@ class IndexPropertyJsonTest(fixtures.DeclarativeMappedTest): jq = s.query(Json).filter(Json.subfield == 'multi').first() eq_(j.id, jq.id) + + def test_nested_property_init(self): + Json = self.classes.Json + + # subfield initializer + j = Json(subfield='a') + eq_(j.json, {'other': {'field': 'a'}}) + + def test_nested_property_set(self): + Json = self.classes.Json + + j = Json() + j.subfield = 'a' + eq_(j.json, {'other': {'field': 'a'}})