MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / expunge

Method expunge

lib/sqlalchemy/orm/session.py:3407–3426  ·  view source on GitHub ↗

Remove the `instance` from this ``Session``. This will free all internal references to the instance. Cascading will be applied according to the *expunge* cascade rule.

(self, instance: object)

Source from the content-addressed store, hash-verified

3405 state._detach(self)
3406
3407 def expunge(self, instance: object) -> None:
3408 """Remove the `instance` from this ``Session``.
3409
3410 This will free all internal references to the instance. Cascading
3411 will be applied according to the *expunge* cascade rule.
3412
3413 """
3414 try:
3415 state = attributes.instance_state(instance)
3416 except exc.NO_STATE as err:
3417 raise exc.UnmappedInstanceError(instance) from err
3418 if state.session_id is not self.hash_key:
3419 raise sa_exc.InvalidRequestError(
3420 "Instance %s is not present in this Session" % state_str(state)
3421 )
3422
3423 cascaded = list(
3424 state.manager.mapper.cascade_iterator("expunge", state)
3425 )
3426 self._expunge_states([state] + [st_ for o, m, st_, dct_ in cascaded])
3427
3428 def _expunge_states(
3429 self, states: Iterable[InstanceState[Any]], to_transient: bool = False

Callers 15

removeFunction · 0.45
set_Function · 0.45
scoped_session.pyFile · 0.45
test_m2o_eventMethod · 0.45
test_detached_raiseMethod · 0.45

Calls 3

_expunge_statesMethod · 0.95
state_strFunction · 0.85
cascade_iteratorMethod · 0.45