MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / identity_key

Function identity_key

lib/sqlalchemy/orm/util.py:401–486  ·  view source on GitHub ↗

r"""Generate "identity key" tuples, as are used as keys in the :attr:`.Session.identity_map` dictionary. This function has several call styles: * ``identity_key(class, ident, identity_token=token)`` This form receives a mapped class and a primary key scalar or tuple as an

(
    class_: Optional[Type[_T]] = None,
    ident: Union[Any, Tuple[Any, ...]] = None,
    *,
    instance: Optional[_T] = None,
    row: Optional[Union[Row[Any], RowMapping]] = None,
    identity_token: Optional[Any] = None,
)

Source from the content-addressed store, hash-verified

399
400
401def identity_key(
402 class_: Optional[Type[_T]] = None,
403 ident: Union[Any, Tuple[Any, ...]] = None,
404 *,
405 instance: Optional[_T] = None,
406 row: Optional[Union[Row[Any], RowMapping]] = None,
407 identity_token: Optional[Any] = None,
408) -> _IdentityKeyType[_T]:
409 r"""Generate "identity key" tuples, as are used as keys in the
410 :attr:`.Session.identity_map` dictionary.
411
412 This function has several call styles:
413
414 * ``identity_key(class, ident, identity_token=token)``
415
416 This form receives a mapped class and a primary key scalar or
417 tuple as an argument.
418
419 E.g.::
420
421 >>> identity_key(MyClass, (1, 2))
422 (<class '__main__.MyClass'>, (1, 2), None)
423
424 :param class: mapped class (must be a positional argument)
425 :param ident: primary key, may be a scalar or tuple argument.
426 :param identity_token: optional identity token
427
428 .. versionadded:: 1.2 added identity_token
429
430
431 * ``identity_key(instance=instance)``
432
433 This form will produce the identity key for a given instance. The
434 instance need not be persistent, only that its primary key attributes
435 are populated (else the key will contain ``None`` for those missing
436 values).
437
438 E.g.::
439
440 >>> instance = MyClass(1, 2)
441 >>> identity_key(instance=instance)
442 (<class '__main__.MyClass'>, (1, 2), None)
443
444 In this form, the given instance is ultimately run though
445 :meth:`_orm.Mapper.identity_key_from_instance`, which will have the
446 effect of performing a database check for the corresponding row
447 if the object is expired.
448
449 :param instance: object instance (must be given as a keyword arg)
450
451 * ``identity_key(class, row=row, identity_token=token)``
452
453 This form is similar to the class/tuple form, except is passed a
454 database result row as a :class:`.Row` or :class:`.RowMapping` object.
455
456 E.g.::
457
458 >>> row = engine.execute(text("select * from table where a=1 and b=2")).first()

Calls 5

class_mapperFunction · 0.85
object_mapperFunction · 0.85
identity_key_from_rowMethod · 0.80