diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-04-09 17:49:16 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-04-09 17:49:16 -0400 |
commit | 9f74861d6d2962cb255887c78d5d0f4cb8891cfa (patch) | |
tree | 6941d52bf00434bd452dc7cf81020d7adadb87cc /lib/sqlalchemy/orm/session.py | |
parent | e8c08b5d7baf1b04cfcec94977822af291f2fa83 (diff) | |
download | sqlalchemy-9f74861d6d2962cb255887c78d5d0f4cb8891cfa.tar.gz |
- Added new utility function :func:`.make_transient_to_detached` which can
be used to manufacture objects that behave as though they were loaded
from a session, then detached. Attributes that aren't present
are marked as expired, and the object can be added to a Session
where it will act like a persistent one. fix #3017
Diffstat (limited to 'lib/sqlalchemy/orm/session.py')
-rw-r--r-- | lib/sqlalchemy/orm/session.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 5bd46691e..a040101bf 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -2361,7 +2361,6 @@ class sessionmaker(_SessionClassMethods): ) - def make_transient(instance): """Make the given instance 'transient'. @@ -2390,6 +2389,41 @@ def make_transient(instance): if state.deleted: del state.deleted +def make_transient_to_detached(instance): + """Make the given transient instance 'detached'. + + All attribute history on the given instance + will be reset as though the instance were freshly loaded + from a query. Missing attributes will be marked as expired. + The primary key attributes of the object, which are required, will be made + into the "key" of the instance. + + The object can then be added to a session, or merged + possibly with the load=False flag, at which point it will look + as if it were loaded that way, without emitting SQL. + + This is a special use case function that differs from a normal + call to :meth:`.Session.merge` in that a given persistent state + can be manufactured without any SQL calls. + + .. versionadded:: 0.9.5 + + .. seealso:: + + :func:`.make_transient` + + """ + state = attributes.instance_state(instance) + if state.session_id or state.key: + raise sa_exc.InvalidRequestError( + "Given object must be transient") + state.key = state.mapper._identity_key_from_state(state) + if state.deleted: + del state.deleted + state._commit_all(state.dict) + state._expire_attributes(state.dict, state.unloaded) + + def object_session(instance): """Return the ``Session`` to which instance belongs. |