MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _identity_lookup

Method _identity_lookup

lib/sqlalchemy/orm/session.py:3067–3124  ·  view source on GitHub ↗

Locate an object in the identity map. Given a primary key identity, constructs an identity key and then looks in the session's identity map. If present, the object may be run through unexpiration rules (e.g. load unloaded attributes, check if was deleted).

(
        self,
        mapper: Mapper[_O],
        primary_key_identity: Union[Any, Tuple[Any, ...]],
        identity_token: Any = None,
        passive: PassiveFlag = PassiveFlag.PASSIVE_OFF,
        lazy_loaded_from: Optional[InstanceState[Any]] = None,
        execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
        bind_arguments: Optional[_BindArguments] = None,
    )

Source from the content-addressed store, hash-verified

3065 return self._query_cls(entities, self, **kwargs)
3066
3067 def _identity_lookup(
3068 self,
3069 mapper: Mapper[_O],
3070 primary_key_identity: Union[Any, Tuple[Any, ...]],
3071 identity_token: Any = None,
3072 passive: PassiveFlag = PassiveFlag.PASSIVE_OFF,
3073 lazy_loaded_from: Optional[InstanceState[Any]] = None,
3074 execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
3075 bind_arguments: Optional[_BindArguments] = None,
3076 ) -> Union[Optional[_O], LoaderCallableStatus]:
3077 """Locate an object in the identity map.
3078
3079 Given a primary key identity, constructs an identity key and then
3080 looks in the session's identity map. If present, the object may
3081 be run through unexpiration rules (e.g. load unloaded attributes,
3082 check if was deleted).
3083
3084 e.g.::
3085
3086 obj = session._identity_lookup(inspect(SomeClass), (1,))
3087
3088 :param mapper: mapper in use
3089 :param primary_key_identity: the primary key we are searching for, as
3090 a tuple.
3091 :param identity_token: identity token that should be used to create
3092 the identity key. Used as is, however overriding subclasses can
3093 repurpose this in order to interpret the value in a special way,
3094 such as if None then look among multiple target tokens.
3095 :param passive: passive load flag passed to
3096 :func:`.loading.get_from_identity`, which impacts the behavior if
3097 the object is found; the object may be validated and/or unexpired
3098 if the flag allows for SQL to be emitted.
3099 :param lazy_loaded_from: an :class:`.InstanceState` that is
3100 specifically asking for this identity as a related identity. Used
3101 for sharding schemes where there is a correspondence between an object
3102 and a related object being lazy-loaded (or otherwise
3103 relationship-loaded).
3104
3105 :return: None if the object is not found in the identity map, *or*
3106 if the object was unexpired and found to have been deleted.
3107 if passive flags disallow SQL and the object is expired, returns
3108 PASSIVE_NO_RESULT. In all other cases the instance is returned.
3109
3110 .. versionchanged:: 1.4.0 - the :meth:`.Session._identity_lookup`
3111 method was moved from :class:`_query.Query` to
3112 :class:`.Session`, to avoid having to instantiate the
3113 :class:`_query.Query` object.
3114
3115
3116 """
3117
3118 key = mapper.identity_key_from_primary_key(
3119 primary_key_identity, identity_token=identity_token
3120 )
3121
3122 # work around: https://github.com/python/typing/discussions/1143
3123 return_value = loading.get_from_identity(self, mapper, key, passive)
3124 return return_value

Callers 2

_get_implMethod · 0.95
_load_for_stateMethod · 0.45

Calls 1

Tested by

no test coverage detected