From db170dd4529c5176d38db649dd75427a932b47fe Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 10 Sep 2017 00:01:18 -0400 Subject: Warn instead of raise for unmapped column that matches on key Modified the change made to the ORM update/delete evaluator in :ticket:`3366` such that if an unmapped column expression is present in the update or delete, if the evaluator can match its name to the mapped columns of the target class, a warning is emitted, rather than raising UnevaluatableError. This is essentially the pre-1.2 behavior, and is to allow migration for applications that are currently relying upon this pattern. However, if the given attribute name cannot be matched to the columns of the mapper, the UnevaluatableError is still raised, which is what was fixed in :ticket:`3366`. Change-Id: I658ed0dbf485b7f8009774f9c12d9912447abd2a Fixes: #4073 --- lib/sqlalchemy/orm/evaluator.py | 16 +++++++++++++--- lib/sqlalchemy/orm/persistence.py | 8 ++++---- 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/orm/evaluator.py b/lib/sqlalchemy/orm/evaluator.py index aff6718d3..3f2a83a02 100644 --- a/lib/sqlalchemy/orm/evaluator.py +++ b/lib/sqlalchemy/orm/evaluator.py @@ -7,6 +7,8 @@ import operator from ..sql import operators +from .. import inspect +from .. import util class UnevaluatableError(Exception): @@ -59,9 +61,17 @@ class EvaluatorCompiler(object): ) key = parentmapper._columntoproperty[clause].key else: - raise UnevaluatableError( - "Cannot evaluate column: %s" % clause - ) + key = clause.key + if self.target_cls and key in inspect(self.target_cls).column_attrs: + util.warn( + "Evaluating non-mapped column expression '%r' onto " + "ORM instances; this is a deprecated use case. Please " + "make use of the actual mapped columns in ORM-evaluated " + "UPDATE / DELETE expressions." % clause) + else: + raise UnevaluatableError( + "Cannot evaluate column: %s" % clause + ) get_corresponding_attr = operator.attrgetter(key) return lambda obj: get_corresponding_attr(obj) diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py index b8fd0c79f..dfa05a85e 100644 --- a/lib/sqlalchemy/orm/persistence.py +++ b/lib/sqlalchemy/orm/persistence.py @@ -1394,11 +1394,11 @@ class BulkEvaluate(BulkUD): self._additional_evaluators(evaluator_compiler) - except evaluator.UnevaluatableError: + except evaluator.UnevaluatableError as err: raise sa_exc.InvalidRequestError( - "Could not evaluate current criteria in Python. " - "Specify 'fetch' or False for the " - "synchronize_session parameter.") + 'Could not evaluate current criteria in Python: "%s". ' + 'Specify \'fetch\' or False for the ' + 'synchronize_session parameter.' % err) # TODO: detect when the where clause is a trivial primary key match self.matched_objects = [ -- cgit v1.2.1