Return an instance based on the given primary key identifier, or ``None`` if not found. E.g.:: my_user = session.get(User, 5) some_object = session.get(VersionedFoo, (5, 10)) some_object = session.get(VersionedFoo, {"id": 5, "version_id": 10})
(
self,
entity: _EntityBindKey[_O],
ident: _PKIdentityArgument,
*,
options: Optional[Sequence[ORMOption]] = None,
populate_existing: bool | None = None,
with_for_update: ForUpdateParameter = None,
identity_token: Optional[Any] = None,
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
bind_arguments: Optional[_BindArguments] = None,
)
| 3705 | self._delete_impl(st_, o, False) |
| 3706 | |
| 3707 | def get( |
| 3708 | self, |
| 3709 | entity: _EntityBindKey[_O], |
| 3710 | ident: _PKIdentityArgument, |
| 3711 | *, |
| 3712 | options: Optional[Sequence[ORMOption]] = None, |
| 3713 | populate_existing: bool | None = None, |
| 3714 | with_for_update: ForUpdateParameter = None, |
| 3715 | identity_token: Optional[Any] = None, |
| 3716 | execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT, |
| 3717 | bind_arguments: Optional[_BindArguments] = None, |
| 3718 | ) -> Optional[_O]: |
| 3719 | """Return an instance based on the given primary key identifier, |
| 3720 | or ``None`` if not found. |
| 3721 | |
| 3722 | E.g.:: |
| 3723 | |
| 3724 | my_user = session.get(User, 5) |
| 3725 | |
| 3726 | some_object = session.get(VersionedFoo, (5, 10)) |
| 3727 | |
| 3728 | some_object = session.get(VersionedFoo, {"id": 5, "version_id": 10}) |
| 3729 | |
| 3730 | .. versionadded:: 1.4 Added :meth:`_orm.Session.get`, which is moved |
| 3731 | from the now legacy :meth:`_orm.Query.get` method. |
| 3732 | |
| 3733 | :meth:`_orm.Session.get` is special in that it provides direct |
| 3734 | access to the identity map of the :class:`.Session`. |
| 3735 | If the given primary key identifier is present |
| 3736 | in the local identity map, the object is returned |
| 3737 | directly from this collection and no SQL is emitted, |
| 3738 | unless the object has been marked fully expired. |
| 3739 | If not present, |
| 3740 | a SELECT is performed in order to locate the object. |
| 3741 | |
| 3742 | :meth:`_orm.Session.get` also will perform a check if |
| 3743 | the object is present in the identity map and |
| 3744 | marked as expired - a SELECT |
| 3745 | is emitted to refresh the object as well as to |
| 3746 | ensure that the row is still present. |
| 3747 | If not, :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised. |
| 3748 | |
| 3749 | :param entity: a mapped class or :class:`.Mapper` indicating the |
| 3750 | type of entity to be loaded. |
| 3751 | |
| 3752 | :param ident: A scalar, tuple, or dictionary representing the |
| 3753 | primary key. For a composite (e.g. multiple column) primary key, |
| 3754 | a tuple or dictionary should be passed. |
| 3755 | |
| 3756 | For a single-column primary key, the scalar calling form is typically |
| 3757 | the most expedient. If the primary key of a row is the value "5", |
| 3758 | the call looks like:: |
| 3759 | |
| 3760 | my_object = session.get(SomeClass, 5) |
| 3761 | |
| 3762 | The tuple form contains primary key values typically in |
| 3763 | the order in which they correspond to the mapped |
| 3764 | :class:`_schema.Table` |