class InstanceState(interfaces.InspectionAttrInfo):
tracks state information at the instance level.
The .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.
.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 .Session
and details
about data on individual attributes. The public API
in order to acquire a .InstanceState
object
is to use the _sa.inspect
system:
>>> from sqlalchemy import inspect >>> insp = inspect(some_mapped_object)
See Also
Class Method | _commit_all_states |
Mass / highly inlined version of commit_all(). |
Class Method | _detach_states |
Undocumented |
Class Method | _instance_level_callable_processor |
Undocumented |
Method | __getstate__ |
Undocumented |
Method | __init__ |
Undocumented |
Method | __setstate__ |
Undocumented |
Method | _cleanup |
Weakref callback cleanup. |
Method | _commit |
Commit attributes. |
Method | _commit_all |
commit all attributes unconditionally. |
Method | _copy_callables |
Undocumented |
Method | _detach |
Undocumented |
Method | _dispose |
Undocumented |
Method | _expire |
Undocumented |
Method | _expire_attributes |
Undocumented |
Method | _get_pending_mutation |
Undocumented |
Method | _initialize_instance |
Undocumented |
Method | _instance_dict |
Undocumented |
Method | _load_expired |
__call__ allows the InstanceState to act as a deferred callable for loading expired attributes, which is also serializable (picklable). |
Method | _modified_event |
Undocumented |
Method | _reset |
Remove the given attribute and any callables associated with it. |
Method | _track_last_known_value |
Track the last known value of a particular key after expiration operations. |
Method | get_history |
Undocumented |
Method | get_impl |
Undocumented |
Method | obj |
Undocumented |
Method | unmodified_intersection |
Return self.unmodified.intersection(keys). |
Class Variable | _deleted |
Undocumented |
Class Variable | _load_pending |
Undocumented |
Class Variable | _orphaned_outside_of_session |
Undocumented |
Class Variable | insert_order |
Undocumented |
Class Variable | is_instance |
True if this object is an instance of .InstanceState . |
Class Variable | runid |
Undocumented |
Instance Variable | _last_known_values |
Undocumented |
Instance Variable | _pending_mutations |
Undocumented |
Instance Variable | _strong_obj |
Undocumented |
Instance Variable | callables |
A namespace where a per-state loader callable can be associated. |
Instance Variable | class_ |
Undocumented |
Instance Variable | committed_state |
Undocumented |
Instance Variable | expired |
Undocumented |
Instance Variable | expired_attributes |
The set of keys which are 'expired' to be loaded by the manager's deferred scalar loader, assuming no pending changes. |
Instance Variable | identity_token |
Undocumented |
Instance Variable | key |
Undocumented |
Instance Variable | load_path |
Undocumented |
Instance Variable | manager |
Undocumented |
Instance Variable | modified |
Undocumented |
Instance Variable | parents |
Undocumented |
Instance Variable | session_id |
Undocumented |
Property | _attached |
Undocumented |
Property | _empty_collections |
Undocumented |
Property | _unloaded_non_object |
Undocumented |
Property | async_session |
Return the owning _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_identity |
Return True if this object has an identity key. |
Property | identity |
No summary |
Property | identity_key |
Return the identity key for the mapped object. |
Property | mapper |
Return the _orm.Mapper used for this mapped object. |
Property | object |
Return the mapped object represented by this .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 .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_expirable |
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_deleted |
Return True if this object is or was previously in the "deleted" state and has not been reverted to persistent. |
Inherited from InspectionAttrInfo
:
Property | info |
Info dictionary associated with the object, allowing user-defined data to be associated with this .InspectionAttr . |
Inherited from InspectionAttr
(via InspectionAttrInfo
):
Class Variable | __slots__ |
Undocumented |
Class Variable | _is_internal_proxy |
True if this object is an internal proxy object. |
Class Variable | is_aliased_class |
True if this object is an instance of .AliasedClass . |
Class Variable | is_attribute |
True if this object is a Python :term:`descriptor`. |
Class Variable | is_bundle |
True if this object is an instance of .Bundle . |
Class Variable | is_clause_element |
True if this object is an instance of _expression.ClauseElement . |
Class Variable | is_mapper |
True if this object is an instance of _orm.Mapper . |
Class Variable | is_property |
True if this object is an instance of .MapperProperty . |
Class Variable | is_selectable |
Return True if this object is an instance of _expression.Selectable . |
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.
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.
Track the last known value of a particular key after expiration operations.
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.
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 _asyncio.AsyncSession
for this instance,
or None if none available.
This attribute is only non-None when the sqlalchemy.ext.asyncio
API is in use for this ORM object. The returned
_asyncio.AsyncSession
object will be a proxy for the
_orm.Session
object that would be returned from the
_orm.InstanceState.session
attribute for this
_orm.InstanceState
.
See Also
Return a namespace representing each attribute on the mapped object, including its current value and history.
The returned object is an instance of .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 .Session.identity_map
of its parent
.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 .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
.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
.Session
, use the .InstanceState.was_deleted
accessor.
See Also
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
_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 .Session.identity_map
mapping. It contains
the identity as returned by .identity
within it.
Return True if the object is :term:`persistent`.
An object that is in the persistent state is guaranteed to
be within the .Session.identity_map
of its parent
.Session
.
.InstanceState.persistent
accessor no longer returns True for an object that was
"deleted" within a flush; use the .InstanceState.deleted
accessor to detect this state. This allows the "persistent"
state to guarantee membership in the identity map.See Also
Return the owning .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.
See Also
_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.
.orm.util.was_deleted
.See Also
.InstanceState.deleted
- refers to the "deleted" state
.orm.util.was_deleted
- standalone function