module documentation

Provides the Session class and related utilities.

Class ORMExecuteState Represents a call to the :meth:`_orm.Session.execute` method, as passed to the :meth:`.SessionEvents.do_orm_execute` event hook.
Class Session Manages persistence operations for ORM-mapped objects.
Class sessionmaker A configurable :class:`.Session` factory.
Class SessionTransaction A :class:`.Session`-level transaction.
Class SessionTransactionOrigin indicates the origin of a :class:`.SessionTransaction`.
Class SessionTransactionState Undocumented
Function close_all_sessions Close all sessions in memory.
Function make_transient Alter the state of the given instance so that it is :term:`transient`.
Function make_transient_to_detached Make the given transient instance :term:`detached`.
Function object_session Return the :class:`.Session` to which the given instance belongs.
Constant ACTIVE Undocumented
Constant CLOSED Undocumented
Constant COMMITTED Undocumented
Constant DEACTIVE Undocumented
Constant PREPARED Undocumented
Variable JoinTransactionMode Undocumented
Class _ConnectionCallableProto a callable that returns a :class:`.Connection` given an instance.
Class _SessionClassMethods Class-level methods for :class:`.Session`, :class:`.sessionmaker`.
Function _state_session Given an :class:`.InstanceState`, return the :class:`.Session` associated, if any.
Type Variable _S Undocumented
Type Variable _T Undocumented
Type Alias _BindArguments Undocumented
Type Alias _EntityBindKey Undocumented
Type Alias _PKIdentityArgument Undocumented
Type Alias _SessionBind Undocumented
Type Alias _SessionBindKey Undocumented
Variable _new_sessionid Undocumented
Variable _sessions Weak-referencing dictionary of :class:`.Session` objects.
def close_all_sessions(): (source)

Close all sessions in memory. This function consults a global registry of all :class:`.Session` objects and calls :meth:`.Session.close` on them, which resets them to a clean state. This function is not for general use but may be useful for test suites within the teardown scheme. .. versionadded:: 1.3

def make_transient(instance: object): (source)

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 function will remove its association with any :class:`.Session` as well as its :attr:`.InstanceState.identity`. The effect is that the object will behave as though it were newly constructed, except retaining any attribute / collection values that were loaded at the time of the call. The :attr:`.InstanceState.deleted` flag is also reset if this object had been deleted as a result of using :meth:`.Session.delete`. .. warning:: :func:`.make_transient` does **not** "unexpire" or otherwise eagerly load ORM-mapped attributes that are not currently loaded at the time the function is called. This includes attributes which: * were expired via :meth:`.Session.expire` * were expired as the natural effect of committing a session transaction, e.g. :meth:`.Session.commit` * are normally :term:`lazy loaded` but are not currently loaded * are "deferred" (see :ref:`orm_queryguide_column_deferral`) and are not yet loaded * were not present in the query which loaded this object, such as that which is common in joined table inheritance and other scenarios. After :func:`.make_transient` is called, unloaded attributes such as those above will normally resolve to the value ``None`` when accessed, or an empty collection for a collection-oriented attribute. As the object is transient and un-associated with any database identity, it will no longer retrieve these values. .. seealso:: :func:`.make_transient_to_detached`

def make_transient_to_detached(instance: object): (source)

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 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. .. seealso:: :func:`.make_transient` :meth:`.Session.enable_relationship_loading`

def object_session(instance: object) -> Optional[Session]: (source)

Return the :class:`.Session` to which the given instance belongs. This is essentially the same as the :attr:`.InstanceState.session` accessor. See that attribute for details.

Undocumented

Undocumented

COMMITTED = (source)

Undocumented

DEACTIVE = (source)

Undocumented

PREPARED = (source)

Undocumented

JoinTransactionMode = (source)

Undocumented

def _state_session(state: InstanceState[Any]) -> Optional[Session]: (source)

Given an :class:`.InstanceState`, return the :class:`.Session` associated, if any.

Undocumented

Value
TypeVar('_S',
        bound='Session')

Undocumented

Value
TypeVar('_T',
        bound=Any)
_BindArguments = (source)

Undocumented

Value
Dict[str, Any]
_EntityBindKey = (source)

Undocumented

Value
Union[Type[_O], 'Mapper[_O]']
_PKIdentityArgument = (source)

Undocumented

Value
Union[Any, Tuple[Any, ...]]
_SessionBind = (source)

Undocumented

Value
Union['Engine', 'Connection']
_SessionBindKey = (source)

Undocumented

Value
Union[Type[Any], 'Mapper[Any]', 'TableClause', str]
_new_sessionid = (source)

Undocumented

Weak-referencing dictionary of :class:`.Session` objects.