MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / identity_key

Function identity_key

lib/sqlalchemy/orm/util.py:410–490  ·  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[Unpack[TupleAny]], RowMapping]] = None,
    identity_token: Optional[Any] = None,
)

Source from the content-addressed store, hash-verified

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

Calls 5

class_mapperFunction · 0.85
object_mapperFunction · 0.85
identity_key_from_rowMethod · 0.80