Return an instance based on the given primary key identifier, or ``None`` if not found. E.g.:: my_user = session.query(User).get(5) some_object = session.query(VersionedFoo).get((5, 10)) some_object = session.query(VersionedFoo).get({"id": 5, "
(self, ident: _PKIdentityArgument)
| 1062 | alternative=class="st">"The method is now available as :meth:`_orm.Session.get`", |
| 1063 | ) |
| 1064 | def get(self, ident: _PKIdentityArgument) -> Optional[_T]: |
| 1065 | class="st">"""Return an instance based on the given primary key identifier, |
| 1066 | or ``None`` if not found. |
| 1067 | |
| 1068 | E.g.:: |
| 1069 | |
| 1070 | my_user = session.query(User).get(5) |
| 1071 | |
| 1072 | some_object = session.query(VersionedFoo).get((5, 10)) |
| 1073 | |
| 1074 | some_object = session.query(VersionedFoo).get({class="st">"id": 5, class="st">"version_id": 10}) |
| 1075 | |
| 1076 | :meth:`_query.Query.get` is special in that it provides direct |
| 1077 | access to the identity map of the owning :class:`.Session`. |
| 1078 | If the given primary key identifier is present |
| 1079 | in the local identity map, the object is returned |
| 1080 | directly from this collection and no SQL is emitted, |
| 1081 | unless the object has been marked fully expired. |
| 1082 | If not present, |
| 1083 | a SELECT is performed in order to locate the object. |
| 1084 | |
| 1085 | :meth:`_query.Query.get` also will perform a check if |
| 1086 | the object is present in the identity map and |
| 1087 | marked as expired - a SELECT |
| 1088 | is emitted to refresh the object as well as to |
| 1089 | ensure that the row is still present. |
| 1090 | If not, :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised. |
| 1091 | |
| 1092 | :meth:`_query.Query.get` is only used to return a single |
| 1093 | mapped instance, not multiple instances or |
| 1094 | individual column constructs, and strictly |
| 1095 | on a single primary key value. The originating |
| 1096 | :class:`_query.Query` must be constructed in this way, |
| 1097 | i.e. against a single mapped entity, |
| 1098 | with no additional filtering criterion. Loading |
| 1099 | options via :meth:`_query.Query.options` may be applied |
| 1100 | however, and will be used if the object is not |
| 1101 | yet locally present. |
| 1102 | |
| 1103 | :param ident: A scalar, tuple, or dictionary representing the |
| 1104 | primary key. For a composite (e.g. multiple column) primary key, |
| 1105 | a tuple or dictionary should be passed. |
| 1106 | |
| 1107 | For a single-column primary key, the scalar calling form is typically |
| 1108 | the most expedient. If the primary key of a row is the value class="st">"5", |
| 1109 | the call looks like:: |
| 1110 | |
| 1111 | my_object = query.get(5) |
| 1112 | |
| 1113 | The tuple form contains primary key values typically in |
| 1114 | the order in which they correspond to the mapped |
| 1115 | :class:`_schema.Table` |
| 1116 | object&class="cm">#x27;s primary key columns, or if the |
| 1117 | :paramref:`_orm.Mapper.primary_key` configuration parameter were |
| 1118 | used, in |
| 1119 | the order used for that parameter. For example, if the primary key |
| 1120 | of a row is represented by the integer |
| 1121 | digits class="st">"5, 10" the call would look like:: |