summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-03-21 17:11:18 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-03-21 17:35:12 -0400
commit711d29f8e4dc096f5083c075a1f64eb38e2d2e4a (patch)
treefe28b0e8f708dc6605ac19062594ff831fb95651 /lib
parentcaeb274e287f514a50524fc9fe4aeedcb3740147 (diff)
downloadsqlalchemy-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')
-rw-r--r--lib/sqlalchemy/orm/attributes.py36
-rw-r--r--lib/sqlalchemy/orm/state.py33
2 files changed, 54 insertions, 15 deletions
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py
index 2b8b38d58..a387e7d76 100644
--- a/lib/sqlalchemy/orm/attributes.py
+++ b/lib/sqlalchemy/orm/attributes.py
@@ -1617,8 +1617,42 @@ def flag_modified(instance, key):
This sets the 'modified' flag on the instance and
establishes an unconditional change event for the given attribute.
+ The attribute must have a value present, else an
+ :class:`.InvalidRequestError` is raised.
+
+ To mark an object "dirty" without referring to any specific attribute
+ so that it is considered within a flush, use the
+ :func:`.attributes.flag_dirty` call.
+
+ .. seealso::
+
+ :func:`.attributes.flag_dirty`
"""
state, dict_ = instance_state(instance), instance_dict(instance)
impl = state.manager[key].impl
- state._modified_event(dict_, impl, NO_VALUE, force=True)
+ state._modified_event(dict_, impl, NO_VALUE, is_userland=True)
+
+
+def flag_dirty(instance):
+ """Mark an instance as 'dirty' without any specific attribute mentioned.
+
+ This is a special operation that will allow the object to travel through
+ the flush process for interception by events such as
+ :meth:`.SessionEvents.before_flush`. Note that no SQL will be emitted in
+ the flush process for an object that has no changes, even if marked dirty
+ via this method. However, a :meth:`.SessionEvents.before_flush` handler
+ will be able to see the object in the :attr:`.Session.dirty` collection and
+ may establish changes on it, which will then be included in the SQL
+ emitted.
+
+ .. versionadded:: 1.2
+
+ .. seealso::
+
+ :func:`.attributes.flag_modified`
+
+ """
+
+ state, dict_ = instance_state(instance), instance_dict(instance)
+ state._modified_event(dict_, None, NO_VALUE, is_userland=True)
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 "