MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / make_transient_to_detached

Function make_transient_to_detached

lib/sqlalchemy/orm/session.py:5411–5447  ·  view source on GitHub ↗

Make the given transient instance :term:`detached`. .. note:: :func:`.make_transient_to_detached` is a special-case function for advanced use cases only. All attribute history on the given instance will be reset as though the instance were freshly loaded from a que

(instance: object)

Source from the content-addressed store, hash-verified

5409
5410
5411def make_transient_to_detached(instance: object) -> None:
5412 """Make the given transient instance :term:`detached`.
5413
5414 .. note::
5415
5416 :func:`.make_transient_to_detached` is a special-case function for
5417 advanced use cases only.
5418
5419 All attribute history on the given instance
5420 will be reset as though the instance were freshly loaded
5421 from a query. Missing attributes will be marked as expired.
5422 The primary key attributes of the object, which are required, will be made
5423 into the "key" of the instance.
5424
5425 The object can then be added to a session, or merged
5426 possibly with the load=False flag, at which point it will look
5427 as if it were loaded that way, without emitting SQL.
5428
5429 This is a special use case function that differs from a normal
5430 call to :meth:`.Session.merge` in that a given persistent state
5431 can be manufactured without any SQL calls.
5432
5433 .. seealso::
5434
5435 :func:`.make_transient`
5436
5437 :meth:`.Session.enable_relationship_loading`
5438
5439 """
5440 state = attributes.instance_state(instance)
5441 if state.session_id or state.key:
5442 raise sa_exc.InvalidRequestError("Given object must be transient")
5443 state.key = state.mapper._identity_key_from_state(state)
5444 if state._deleted:
5445 del state._deleted
5446 state._commit_all(state.dict)
5447 state._expire_attributes(state.dict, state.unloaded)
5448
5449
5450def object_session(instance: object) -> Optional[Session]:

Calls 3

_commit_allMethod · 0.80
_expire_attributesMethod · 0.80