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 = False,
with_for_update: ForUpdateParameter = None,
identity_token: Optional[Any] = None,
execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
bind_arguments: Optional[_BindArguments] = None,
)
| 3589 | self._delete_impl(st_, o, False) |
| 3590 | |
| 3591 | def get( |
| 3592 | self, |
| 3593 | entity: _EntityBindKey[_O], |
| 3594 | ident: _PKIdentityArgument, |
| 3595 | *, |
| 3596 | options: Optional[Sequence[ORMOption]] = None, |
| 3597 | populate_existing: bool = False, |
| 3598 | with_for_update: ForUpdateParameter = None, |
| 3599 | identity_token: Optional[Any] = None, |
| 3600 | execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT, |
| 3601 | bind_arguments: Optional[_BindArguments] = None, |
| 3602 | ) -> Optional[_O]: |
| 3603 | """Return an instance based on the given primary key identifier, |
| 3604 | or ``None`` if not found. |
| 3605 | |
| 3606 | E.g.:: |
| 3607 | |
| 3608 | my_user = session.get(User, 5) |
| 3609 | |
| 3610 | some_object = session.get(VersionedFoo, (5, 10)) |
| 3611 | |
| 3612 | some_object = session.get(VersionedFoo, {"id": 5, "version_id": 10}) |
| 3613 | |
| 3614 | .. versionadded:: 1.4 Added :meth:`_orm.Session.get`, which is moved |
| 3615 | from the now legacy :meth:`_orm.Query.get` method. |
| 3616 | |
| 3617 | :meth:`_orm.Session.get` is special in that it provides direct |
| 3618 | access to the identity map of the :class:`.Session`. |
| 3619 | If the given primary key identifier is present |
| 3620 | in the local identity map, the object is returned |
| 3621 | directly from this collection and no SQL is emitted, |
| 3622 | unless the object has been marked fully expired. |
| 3623 | If not present, |
| 3624 | a SELECT is performed in order to locate the object. |
| 3625 | |
| 3626 | :meth:`_orm.Session.get` also will perform a check if |
| 3627 | the object is present in the identity map and |
| 3628 | marked as expired - a SELECT |
| 3629 | is emitted to refresh the object as well as to |
| 3630 | ensure that the row is still present. |
| 3631 | If not, :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised. |
| 3632 | |
| 3633 | :param entity: a mapped class or :class:`.Mapper` indicating the |
| 3634 | type of entity to be loaded. |
| 3635 | |
| 3636 | :param ident: A scalar, tuple, or dictionary representing the |
| 3637 | primary key. For a composite (e.g. multiple column) primary key, |
| 3638 | a tuple or dictionary should be passed. |
| 3639 | |
| 3640 | For a single-column primary key, the scalar calling form is typically |
| 3641 | the most expedient. If the primary key of a row is the value "5", |
| 3642 | the call looks like:: |
| 3643 | |
| 3644 | my_object = session.get(SomeClass, 5) |
| 3645 | |
| 3646 | The tuple form contains primary key values typically in |
| 3647 | the order in which they correspond to the mapped |
| 3648 | :class:`_schema.Table` |