diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-03-21 17:11:18 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2017-03-21 17:35:12 -0400 |
| commit | 711d29f8e4dc096f5083c075a1f64eb38e2d2e4a (patch) | |
| tree | fe28b0e8f708dc6605ac19062594ff831fb95651 /lib/sqlalchemy/orm/state.py | |
| parent | caeb274e287f514a50524fc9fe4aeedcb3740147 (diff) | |
| download | sqlalchemy-711d29f8e4dc096f5083c075a1f64eb38e2d2e4a.tar.gz | |
Raise on flag_modified() for non-present attribute
The :func:`.attributes.flag_modified` function now raises
:class:`.InvalidRequestError` if the named attribute key is not
present within the object, as this is assumed to be present
in the flush process. To mark an object "dirty" for a flush
without referring to any specific attribute, the
:func:`.attributes.flag_dirty` function may be used.
Change-Id: I6c64e4d253c239e38632f38c27bb16e68fe8dfbe
Fixes: #3753
Diffstat (limited to 'lib/sqlalchemy/orm/state.py')
| -rw-r--r-- | lib/sqlalchemy/orm/state.py | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/lib/sqlalchemy/orm/state.py b/lib/sqlalchemy/orm/state.py index 0fba24004..1781a41e9 100644 --- a/lib/sqlalchemy/orm/state.py +++ b/lib/sqlalchemy/orm/state.py @@ -15,6 +15,7 @@ defines a large part of the ORM's interactivity. import weakref from .. import util from .. import inspection +from .. import exc as sa_exc from . import exc as orm_exc, interfaces from .path_registry import PathRegistry from .base import PASSIVE_NO_RESULT, SQL_OK, NEVER_SET, ATTR_WAS_SET, \ @@ -634,19 +635,23 @@ class InstanceState(interfaces.InspectionAttr): return None def _modified_event( - self, dict_, attr, previous, collection=False, force=False): - if not attr.send_modified_events: - return - if attr.key not in self.committed_state or force: - if collection: - if previous is NEVER_SET: - if attr.key in dict_: - previous = dict_[attr.key] - - if previous not in (None, NO_VALUE, NEVER_SET): - previous = attr.copy(previous) - - self.committed_state[attr.key] = previous + self, dict_, attr, previous, collection=False, is_userland=False): + if attr: + if not attr.send_modified_events: + return + if is_userland and attr.key not in dict_: + raise sa_exc.InvalidRequestError( + "Can't flag attribute '%s' modified; it's not present in " + "the object state" % attr.key) + if attr.key not in self.committed_state or is_userland: + if collection: + if previous is NEVER_SET: + if attr.key in dict_: + previous = dict_[attr.key] + + if previous not in (None, NO_VALUE, NEVER_SET): + previous = attr.copy(previous) + self.committed_state[attr.key] = previous # assert self._strong_obj is None or self.modified @@ -664,7 +669,7 @@ class InstanceState(interfaces.InspectionAttr): if self.session_id: self._strong_obj = inst - if inst is None: + if inst is None and attr: raise orm_exc.ObjectDereferencedError( "Can't emit change event for attribute '%s' - " "parent object of type %s has been garbage " |
