MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / expunge

Method expunge

lib/sqlalchemy/orm/session.py:3305–3324  ·  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

3303 state._detach(self)
3304
3305 def expunge(self, instance: object) -> None:
3306 """Remove the `instance` from this ``Session``.
3307
3308 This will free all internal references to the instance. Cascading
3309 will be applied according to the *expunge* cascade rule.
3310
3311 """
3312 try:
3313 state = attributes.instance_state(instance)
3314 except exc.NO_STATE as err:
3315 raise exc.UnmappedInstanceError(instance) from err
3316 if state.session_id is not self.hash_key:
3317 raise sa_exc.InvalidRequestError(
3318 "Instance %s is not present in this Session" % state_str(state)
3319 )
3320
3321 cascaded = list(
3322 state.manager.mapper.cascade_iterator("expunge", state)
3323 )
3324 self._expunge_states([state] + [st_ for o, m, st_, dct_ in cascaded])
3325
3326 def _expunge_states(
3327 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