Alter the state of the given instance so that it is :term:`transient`. .. note:: :func:`.make_transient` is a special-case function for advanced use cases only. The given mapped instance is assumed to be in the :term:`persistent` or :term:`detached` state. The functi
(instance: object)
| 5344 | |
| 5345 | |
| 5346 | def make_transient(instance: object) -> None: |
| 5347 | """Alter the state of the given instance so that it is :term:`transient`. |
| 5348 | |
| 5349 | .. note:: |
| 5350 | |
| 5351 | :func:`.make_transient` is a special-case function for |
| 5352 | advanced use cases only. |
| 5353 | |
| 5354 | The given mapped instance is assumed to be in the :term:`persistent` or |
| 5355 | :term:`detached` state. The function will remove its association with any |
| 5356 | :class:`.Session` as well as its :attr:`.InstanceState.identity`. The |
| 5357 | effect is that the object will behave as though it were newly constructed, |
| 5358 | except retaining any attribute / collection values that were loaded at the |
| 5359 | time of the call. The :attr:`.InstanceState.deleted` flag is also reset |
| 5360 | if this object had been deleted as a result of using |
| 5361 | :meth:`.Session.delete`. |
| 5362 | |
| 5363 | .. warning:: |
| 5364 | |
| 5365 | :func:`.make_transient` does **not** "unexpire" or otherwise eagerly |
| 5366 | load ORM-mapped attributes that are not currently loaded at the time |
| 5367 | the function is called. This includes attributes which: |
| 5368 | |
| 5369 | * were expired via :meth:`.Session.expire` |
| 5370 | |
| 5371 | * were expired as the natural effect of committing a session |
| 5372 | transaction, e.g. :meth:`.Session.commit` |
| 5373 | |
| 5374 | * are normally :term:`lazy loaded` but are not currently loaded |
| 5375 | |
| 5376 | * are "deferred" (see :ref:`orm_queryguide_column_deferral`) and are |
| 5377 | not yet loaded |
| 5378 | |
| 5379 | * were not present in the query which loaded this object, such as that |
| 5380 | which is common in joined table inheritance and other scenarios. |
| 5381 | |
| 5382 | After :func:`.make_transient` is called, unloaded attributes such |
| 5383 | as those above will normally resolve to the value ``None`` when |
| 5384 | accessed, or an empty collection for a collection-oriented attribute. |
| 5385 | As the object is transient and un-associated with any database |
| 5386 | identity, it will no longer retrieve these values. |
| 5387 | |
| 5388 | .. seealso:: |
| 5389 | |
| 5390 | :func:`.make_transient_to_detached` |
| 5391 | |
| 5392 | """ |
| 5393 | state = attributes.instance_state(instance) |
| 5394 | s = _state_session(state) |
| 5395 | if s: |
| 5396 | s._expunge_states([state]) |
| 5397 | |
| 5398 | # remove expired state |
| 5399 | state.expired_attributes.clear() |
| 5400 | |
| 5401 | # remove deferred callables |
| 5402 | if state.callables: |
| 5403 | del state.callables |