query.update() does not accept instrumented attributes

Issue #1262 resolved
jek created an issue

Query.update's docstring reads:

Updates rows matched by this query in the database. The values parameter takes a dictionary with object attributes as keys and literal values or sql expressions as value.

but currently only string keys are accepted. (column name? property name?) Either the doc should be updated or Class.attr should be accepted, allowing the below test to pass:

Index: test/orm/query.py
===================================================================
--- test/orm/query.py   (revision 5527)
+++ test/orm/query.py   (working copy)
@@ -2665,6 +2665,17 @@
         eq_(sess.query(User.age).order_by(User.id).all(), zip([25,37,29,27](25,37,29,27)))

     @testing.resolve_artifact_names
+    def test_update_by_attribute(self):
+        sess = create_session(bind=testing.db, autocommit=False)
+        
+        john,jack,jill,jane = sess.query(User).order_by(User.id).all()
+        sess.query(User).filter(User.age > 29).update(
+            {User.age: User.age - 10}, synchronize_session='evaluate')
+
+        eq_([jack.age, jill.age, jane.age](john.age,), [25,37,29,27](25,37,29,27))
+        eq_(sess.query(User.age).order_by(User.id).all(), zip([25,37,29,27](25,37,29,27)))
+
+    @testing.resolve_artifact_names
     def test_update_with_bindparams(self):
         sess = create_session(bind=testing.db, autocommit=False)

Comments (3)

  1. Log in to comment