summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeong YunWon <jeong@youknowone.org>2017-01-27 23:29:59 +0900
committerMike Bayer <mike_mp@zzzcomputing.com>2017-01-30 12:59:54 -0500
commitf411cac35001e0d40a6217846d3df40f676a2d4d (patch)
tree8fea7df45be5e7099bd43ea9b1b932f40030d651
parent1c578a710f14568856dad6a1c0bad1269b4108c4 (diff)
downloadsqlalchemy-f411cac35001e0d40a6217846d3df40f676a2d4d.tar.gz
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
-rw-r--r--doc/build/changelog/changelog_11.rst8
-rw-r--r--lib/sqlalchemy/ext/indexable.py6
-rw-r--r--test/ext/test_indexable.py14
3 files changed, 26 insertions, 2 deletions
diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst
index 906f570ff..72948aaae 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 8298a65a9..b1ce12923 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 3df49cf86..76a99aa6d 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'}})