]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix nested index_property setter when there is no container value
authorJeong YunWon <jeong@youknowone.org>
Fri, 27 Jan 2017 14:29:59 +0000 (23:29 +0900)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 30 Jan 2017 17:59:54 +0000 (12:59 -0500)
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

doc/build/changelog/changelog_11.rst
lib/sqlalchemy/ext/indexable.py
test/ext/test_indexable.py

index 906f570ff87bd0070551b2614cf5858852103e52..72948aaaef396f7f084ba4defd9f79b57b539934 100644 (file)
 .. 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
index 8298a65a9a56cb53ca91d053b5516072d16f6d07..b1ce12923128dc3efab9b2189058723c23588958 100644 (file)
@@ -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
index 3df49cf86a9b6d391723ddb2223f38f56c31d8d8..76a99aa6dc0c1f06def5091af27f0273fe5a7fcb 100644 (file)
@@ -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'}})