Expire and refresh attributes on the given instance. The selected attributes will first be expired as they would when using :meth:`_orm.Session.expire`; then a SELECT statement will be issued to the database to refresh column-oriented attributes with the current valu
(
self,
instance: object,
attribute_names: Optional[Iterable[str]] = None,
with_for_update: ForUpdateParameter = None,
)
| 3175 | raise e.with_traceback(sys.exc_info()[2]) |
| 3176 | |
| 3177 | def refresh( |
| 3178 | self, |
| 3179 | instance: object, |
| 3180 | attribute_names: Optional[Iterable[str]] = None, |
| 3181 | with_for_update: ForUpdateParameter = None, |
| 3182 | ) -> None: |
| 3183 | """Expire and refresh attributes on the given instance. |
| 3184 | |
| 3185 | The selected attributes will first be expired as they would when using |
| 3186 | :meth:`_orm.Session.expire`; then a SELECT statement will be issued to |
| 3187 | the database to refresh column-oriented attributes with the current |
| 3188 | value available in the current transaction. |
| 3189 | |
| 3190 | :func:`_orm.relationship` oriented attributes will also be immediately |
| 3191 | loaded if they were already eagerly loaded on the object, using the |
| 3192 | same eager loading strategy that they were loaded with originally. |
| 3193 | |
| 3194 | .. versionadded:: 1.4 - the :meth:`_orm.Session.refresh` method |
| 3195 | can also refresh eagerly loaded attributes. |
| 3196 | |
| 3197 | :func:`_orm.relationship` oriented attributes that would normally |
| 3198 | load using the ``select`` (or "lazy") loader strategy will also |
| 3199 | load **if they are named explicitly in the attribute_names |
| 3200 | collection**, emitting a SELECT statement for the attribute using the |
| 3201 | ``immediate`` loader strategy. If lazy-loaded relationships are not |
| 3202 | named in :paramref:`_orm.Session.refresh.attribute_names`, then |
| 3203 | they remain as "lazy loaded" attributes and are not implicitly |
| 3204 | refreshed. |
| 3205 | |
| 3206 | .. versionchanged:: 2.0.4 The :meth:`_orm.Session.refresh` method |
| 3207 | will now refresh lazy-loaded :func:`_orm.relationship` oriented |
| 3208 | attributes for those which are named explicitly in the |
| 3209 | :paramref:`_orm.Session.refresh.attribute_names` collection. |
| 3210 | |
| 3211 | .. tip:: |
| 3212 | |
| 3213 | While the :meth:`_orm.Session.refresh` method is capable of |
| 3214 | refreshing both column and relationship oriented attributes, its |
| 3215 | primary focus is on refreshing of local column-oriented attributes |
| 3216 | on a single instance. For more open ended "refresh" functionality, |
| 3217 | including the ability to refresh the attributes on many objects at |
| 3218 | once while having explicit control over relationship loader |
| 3219 | strategies, use the |
| 3220 | :ref:`populate existing <orm_queryguide_populate_existing>` feature |
| 3221 | instead. |
| 3222 | |
| 3223 | Note that a highly isolated transaction will return the same values as |
| 3224 | were previously read in that same transaction, regardless of changes |
| 3225 | in database state outside of that transaction. Refreshing |
| 3226 | attributes usually only makes sense at the start of a transaction |
| 3227 | where database rows have not yet been accessed. |
| 3228 | |
| 3229 | :param attribute_names: optional. An iterable collection of |
| 3230 | string attribute names indicating a subset of attributes to |
| 3231 | be refreshed. |
| 3232 | |
| 3233 | :param with_for_update: optional boolean ``True`` indicating FOR UPDATE |
| 3234 | should be used, or may be a dictionary containing flags to |