class InstanceState(interfaces.InspectionAttrInfo, Generic[
tracks state information at the instance level. The :class:`.InstanceState` is a key object used by the SQLAlchemy ORM in order to track the state of an object; it is created the moment an object is instantiated, typically as a result of :term:`instrumentation` which SQLAlchemy applies to the ``__init__()`` method of the class. :class:`.InstanceState` is also a semi-public object, available for runtime inspection as to the state of a mapped instance, including information such as its current status within a particular :class:`.Session` and details about data on individual attributes. The public API in order to acquire a :class:`.InstanceState` object is to use the :func:`_sa.inspect` system:: >>> from sqlalchemy import inspect >>> insp = inspect(some_mapped_object) >>> insp.attrs.nickname.history History(added=['new nickname'], unchanged=(), deleted=['nickname']) .. seealso:: :ref:`orm_mapper_inspection_instancestate`
Method | __getstate__ |
Undocumented |
Method | __init__ |
Undocumented |
Method | __setstate__ |
Undocumented |
Method | get |
Undocumented |
Method | get |
Undocumented |
Method | unmodified |
Return self.unmodified.intersection(keys). |
Class Variable | __slots__ |
Undocumented |
Class Variable | insert |
Undocumented |
Class Variable | is |
True if this object is an instance of :class:`.InstanceState`. |
Class Variable | key |
Undocumented |
Class Variable | load |
Undocumented |
Class Variable | runid |
Undocumented |
Instance Variable | callables |
A namespace where a per-state loader callable can be associated. |
Instance Variable | class_ |
Undocumented |
Instance Variable | committed |
Undocumented |
Instance Variable | expired |
Undocumented |
Instance Variable | expired |
The set of keys which are 'expired' to be loaded by the manager's deferred scalar loader, assuming no pending changes. |
Instance Variable | identity |
Undocumented |
Instance Variable | load |
Undocumented |
Instance Variable | manager |
Undocumented |
Instance Variable | modified |
Undocumented |
Instance Variable | obj |
Undocumented |
Instance Variable | parents |
Undocumented |
Instance Variable | session |
Undocumented |
Property | async |
Return the owning :class:`_asyncio.AsyncSession` for this instance, or ``None`` if none available. |
Property | attrs |
Return a namespace representing each attribute on the mapped object, including its current value and history. |
Property | deleted |
Return ``True`` if the object is :term:`deleted`. |
Property | detached |
Return ``True`` if the object is :term:`detached`. |
Property | dict |
Return the instance dict used by the object. |
Property | has |
Return ``True`` if this object has an identity key. |
Property | identity |
Return the mapped identity of the mapped object. This is the primary key identity as persisted by the ORM which can always be passed directly to :meth:`_query.Query.get`. |
Property | identity |
Return the identity key for the mapped object. |
Property | mapper |
Return the :class:`_orm.Mapper` used for this mapped object. |
Property | object |
Return the mapped object represented by this :class:`.InstanceState`. |
Property | pending |
Return ``True`` if the object is :term:`pending`. |
Property | persistent |
Return ``True`` if the object is :term:`persistent`. |
Property | session |
Return the owning :class:`.Session` for this instance, or ``None`` if none available. |
Property | transient |
Return ``True`` if the object is :term:`transient`. |
Property | unloaded |
Return the set of keys which do not have a loaded value. |
Property | unloaded |
Return the set of keys which do not have a loaded value. |
Property | unmodified |
Return the set of keys which have no uncommitted changes |
Property | was |
Return True if this object is or was previously in the "deleted" state and has not been reverted to persistent. |
Class Method | _commit |
Mass / highly inlined version of commit_all(). |
Class Method | _detach |
Undocumented |
Class Method | _instance |
Undocumented |
Method | _cleanup |
Weakref callback cleanup. |
Method | _commit |
Commit attributes. |
Method | _commit |
commit all attributes unconditionally. |
Method | _copy |
Undocumented |
Method | _detach |
Undocumented |
Method | _dispose |
Undocumented |
Method | _expire |
Undocumented |
Method | _expire |
Undocumented |
Method | _get |
Undocumented |
Method | _initialize |
Undocumented |
Method | _instance |
default 'weak reference' for _instance_dict |
Method | _load |
__call__ allows the InstanceState to act as a deferred callable for loading expired attributes, which is also serializable (picklable). |
Method | _modified |
Undocumented |
Method | _reset |
Remove the given attribute and any callables associated with it. |
Method | _track |
Track the last known value of a particular key after expiration operations. |
Class Variable | _deleted |
Undocumented |
Class Variable | _load |
Undocumented |
Class Variable | _orphaned |
Undocumented |
Instance Variable | _last |
Undocumented |
Instance Variable | _pending |
Undocumented |
Instance Variable | _strong |
Undocumented |
Property | _attached |
Undocumented |
Property | _empty |
Undocumented |
Property | _unloaded |
Undocumented |
Inherited from InspectionAttrInfo
:
Property | info |
Info dictionary associated with the object, allowing user-defined data to be associated with this :class:`.InspectionAttr`. |
Inherited from InspectionAttr
(via InspectionAttrInfo
):
Class Variable | is |
True if this object is an instance of :class:`.AliasedClass`. |
Class Variable | is |
True if this object is a Python :term:`descriptor`. |
Class Variable | is |
True if this object is an instance of :class:`.Bundle`. |
Class Variable | is |
True if this object is an instance of :class:`_expression.ClauseElement`. |
Class Variable | is |
True if this object is an instance of :class:`_orm.Mapper`. |
Class Variable | is |
True if this object is an instance of :class:`.MapperProperty`. |
Class Variable | is |
Return True if this object is an instance of :class:`_expression.Selectable`. |
Class Variable | _is |
True if this object is an internal proxy object. |
A namespace where a per-state loader callable can be associated. In SQLAlchemy 1.0, this is only used for lazy loaders / deferred loaders that were set up via query option. Previously, callables was used also to indicate expired attributes by storing a link to the InstanceState itself in this dictionary. This role is now handled by the expired_attributes set.
The set of keys which are 'expired' to be loaded by the manager's deferred scalar loader, assuming no pending changes. see also the ``unmodified`` collection which is intersected against this set when a refresh operation occurs.
Return the owning :class:`_asyncio.AsyncSession` for this instance, or ``None`` if none available. This attribute is only non-None when the :mod:`sqlalchemy.ext.asyncio` API is in use for this ORM object. The returned :class:`_asyncio.AsyncSession` object will be a proxy for the :class:`_orm.Session` object that would be returned from the :attr:`_orm.InstanceState.session` attribute for this :class:`_orm.InstanceState`. .. versionadded:: 1.4.18 .. seealso:: :ref:`asyncio_toplevel`
Return a namespace representing each attribute on the mapped object, including its current value and history. The returned object is an instance of :class:`.AttributeState`. This object allows inspection of the current data within an attribute as well as attribute history since the last flush.
Return ``True`` if the object is :term:`deleted`. An object that is in the deleted state is guaranteed to not be within the :attr:`.Session.identity_map` of its parent :class:`.Session`; however if the session's transaction is rolled back, the object will be restored to the persistent state and the identity map. .. note:: The :attr:`.InstanceState.deleted` attribute refers to a specific state of the object that occurs between the "persistent" and "detached" states; once the object is :term:`detached`, the :attr:`.InstanceState.deleted` attribute **no longer returns True**; in order to detect that a state was deleted, regardless of whether or not the object is associated with a :class:`.Session`, use the :attr:`.InstanceState.was_deleted` accessor. .. versionadded: 1.1 .. seealso:: :ref:`session_object_states`
Return the instance dict used by the object. Under normal circumstances, this is always synonymous with the ``__dict__`` attribute of the mapped object, unless an alternative instrumentation system has been configured. In the case that the actual object has been garbage collected, this accessor returns a blank dictionary.
Return ``True`` if this object has an identity key. This should always have the same value as the expression ``state.persistent`` or ``state.detached``.
Return the mapped identity of the mapped object. This is the primary key identity as persisted by the ORM which can always be passed directly to :meth:`_query.Query.get`. Returns ``None`` if the object has no primary key identity. .. note:: An object which is :term:`transient` or :term:`pending` does **not** have a mapped identity until it is flushed, even if its attributes include primary key values.
Return the identity key for the mapped object. This is the key used to locate the object within the :attr:`.Session.identity_map` mapping. It contains the identity as returned by :attr:`.identity` within it.
Return the mapped object represented by this :class:`.InstanceState`. Returns None if the object has been garbage collected
Return ``True`` if the object is :term:`persistent`. An object that is in the persistent state is guaranteed to be within the :attr:`.Session.identity_map` of its parent :class:`.Session`. .. versionchanged:: 1.1 The :attr:`.InstanceState.persistent` accessor no longer returns True for an object that was "deleted" within a flush; use the :attr:`.InstanceState.deleted` accessor to detect this state. This allows the "persistent" state to guarantee membership in the identity map. .. seealso:: :ref:`session_object_states`
Return the owning :class:`.Session` for this instance, or ``None`` if none available. Note that the result here can in some cases be *different* from that of ``obj in session``; an object that's been deleted will report as not ``in session``, however if the transaction is still in progress, this attribute will still refer to that session. Only when the transaction is completed does the object become fully detached under normal circumstances. .. seealso:: :attr:`_orm.InstanceState.async_session`
Return the set of keys which do not have a loaded value. This includes expired attributes and any other attribute that was never populated or modified.
Return the set of keys which do not have a loaded value. This includes expired attributes and any other attribute that was never populated or modified.
Return True if this object is or was previously in the "deleted" state and has not been reverted to persistent. This flag returns True once the object was deleted in flush. When the object is expunged from the session either explicitly or via transaction commit and enters the "detached" state, this flag will continue to report True. .. versionadded:: 1.1 - added a local method form of :func:`.orm.util.was_deleted`. .. seealso:: :attr:`.InstanceState.deleted` - refers to the "deleted" state :func:`.orm.util.was_deleted` - standalone function :ref:`session_object_states`
def _commit_all_states(self, iter_:
Iterable[ Tuple[ InstanceState[ Any], _InstanceDict]]
, instance_dict: Optional[ IdentityMap]
= None):
(source)
¶
Mass / highly inlined version of commit_all().
def _detach_states(self, states:
Iterable[ InstanceState[ _O]]
, session: Session
, to_transient: bool
= False):
(source)
¶
Undocumented
def _instance_level_callable_processor(cls, manager:
ClassManager[ _O]
, fn: _LoaderCallable
, key: Any
) -> _InstallLoaderCallableProto[ _O]
:
(source)
¶
Undocumented
Weakref callback cleanup. This callable cleans out the state when it is being garbage collected. this _cleanup **assumes** that there are no strong refs to us! Will not work otherwise!
Commit attributes. This is used by a partial-attribute load operation to mark committed those attributes which were refreshed from the database. Attributes marked as "expired" can potentially remain "expired" after this step if a value was not populated in state.dict.
_InstanceDict
, instance_dict: Optional[ IdentityMap]
= None):
(source)
¶
commit all attributes unconditionally. This is used after a flush() or a full load/refresh to remove all pending state from the instance. - all attributes are marked as "committed" - the "strong dirty reference" is removed - the "modified" flag is set to False - any "expired" markers for scalar attributes loaded are removed. - lazy load callables for objects / collections *stay* Attributes marked as "expired" can potentially remain "expired" after this step if a value was not populated in state.dict.
_InstanceDict
, attribute_names: Iterable[ str]
, no_loader: bool
= False):
(source)
¶
Undocumented
InstanceState[ _O]
, passive: PassiveFlag
) -> LoaderCallableStatus
:
(source)
¶
__call__ allows the InstanceState to act as a deferred callable for loading expired attributes, which is also serializable (picklable).
_InstanceDict
, attr: Optional[ AttributeImpl]
, previous: Any
, collection: bool
= False, is_userland: bool
= False):
(source)
¶
Undocumented
@util.preload_module(
_attached:
bool
=
(source)
¶
Undocumented