:version: 0.9.7
:released:
+ .. change::
+ :tags: bug, orm
+ :tickets: 3117
+
+ The "evaulator" for query.update()/delete() won't work with multi-table
+ updates, and needs to be set to `synchronize_session=False` or
+ `synchronize_session='fetch'`; a warning is now emitted. In
+ 1.0 this will be promoted to a full exception.
+
.. change::
:tags: feature, postgresql
:versions: 1.0.0
import operator
from ..sql import operators
-
+from .. import util
class UnevaluatableError(Exception):
pass
class EvaluatorCompiler(object):
+ def __init__(self, target_cls=None):
+ self.target_cls = target_cls
+
def process(self, clause):
meth = getattr(self, "visit_%s" % clause.__visit_name__, None)
if not meth:
def visit_column(self, clause):
if 'parentmapper' in clause._annotations:
- key = clause._annotations['parentmapper'].\
- _columntoproperty[clause].key
+ parentmapper = clause._annotations['parentmapper']
+ if self.target_cls and not issubclass(
+ self.target_cls, parentmapper.class_):
+ util.warn(
+ "Can't do in-Python evaluation of criteria against "
+ "alternate class %s; "
+ "expiration of objects will not be accurate "
+ "and/or may fail. synchronize_session should be set to "
+ "False or 'fetch'. "
+ "This warning will be an exception "
+ "in 1.0." % parentmapper.class_
+ )
+ key = parentmapper._columntoproperty[clause].key
else:
key = clause.key
+
get_corresponding_attr = operator.attrgetter(key)
return lambda obj: get_corresponding_attr(obj)
def _do_pre_synchronize(self):
query = self.query
+ target_cls = query._mapper_zero().class_
+
try:
- evaluator_compiler = evaluator.EvaluatorCompiler()
+ evaluator_compiler = evaluator.EvaluatorCompiler(target_cls)
if query.whereclause is not None:
eval_condition = evaluator_compiler.process(
query.whereclause)
"Could not evaluate current criteria in Python. "
"Specify 'fetch' or False for the "
"synchronize_session parameter.")
- target_cls = query._mapper_zero().class_
#TODO: detect when the where clause is a trivial primary key match
self.matched_objects = [
test_needs_autoincrement=True),
Column('name', String(32)),
Column('age', Integer))
-
@classmethod
def setup_classes(cls):
class User(cls.Comparable):
pass
-
@classmethod
def insert_data(cls):
users = cls.tables.users
])
)
+ def test_no_eval_against_multi_table_criteria(self):
+ User = self.classes.User
+ Document = self.classes.Document
+
+ s = Session()
+
+ q = s.query(User).filter(User.id == Document.user_id)
+ assert_raises_message(
+ exc.SAWarning,
+ r"Can't do in-Python evaluation of criteria against alternate "
+ r"class .*Document.*; "
+ "expiration of objects will not be accurate "
+ "and/or may fail. synchronize_session should be set to "
+ "False or 'fetch'. "
+ "This warning will be an exception "
+ "in 1.0.",
+ q.update,
+ {"name": "ed"}
+ )
+
+
@testing.requires.update_where_target_in_subquery
def test_update_using_in(self):
Document = self.classes.Document
filter(User.id == 2).update({
Document.samename: 'd_samename',
User.samename: 'u_samename'
- }
+ }, synchronize_session=False
)
eq_(
s.query(User.id, Document.samename, User.samename).