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,
)
| 3073 | raise e.with_traceback(sys.exc_info()[2]) |
| 3074 | |
| 3075 | def refresh( |
| 3076 | self, |
| 3077 | instance: object, |
| 3078 | attribute_names: Optional[Iterable[str]] = None, |
| 3079 | with_for_update: ForUpdateParameter = None, |
| 3080 | ) -> None: |
| 3081 | """Expire and refresh attributes on the given instance. |
| 3082 | |
| 3083 | The selected attributes will first be expired as they would when using |
| 3084 | :meth:`_orm.Session.expire`; then a SELECT statement will be issued to |
| 3085 | the database to refresh column-oriented attributes with the current |
| 3086 | value available in the current transaction. |
| 3087 | |
| 3088 | :func:`_orm.relationship` oriented attributes will also be immediately |
| 3089 | loaded if they were already eagerly loaded on the object, using the |
| 3090 | same eager loading strategy that they were loaded with originally. |
| 3091 | |
| 3092 | .. versionadded:: 1.4 - the :meth:`_orm.Session.refresh` method |
| 3093 | can also refresh eagerly loaded attributes. |
| 3094 | |
| 3095 | :func:`_orm.relationship` oriented attributes that would normally |
| 3096 | load using the ``select`` (or "lazy") loader strategy will also |
| 3097 | load **if they are named explicitly in the attribute_names |
| 3098 | collection**, emitting a SELECT statement for the attribute using the |
| 3099 | ``immediate`` loader strategy. If lazy-loaded relationships are not |
| 3100 | named in :paramref:`_orm.Session.refresh.attribute_names`, then |
| 3101 | they remain as "lazy loaded" attributes and are not implicitly |
| 3102 | refreshed. |
| 3103 | |
| 3104 | .. versionchanged:: 2.0.4 The :meth:`_orm.Session.refresh` method |
| 3105 | will now refresh lazy-loaded :func:`_orm.relationship` oriented |
| 3106 | attributes for those which are named explicitly in the |
| 3107 | :paramref:`_orm.Session.refresh.attribute_names` collection. |
| 3108 | |
| 3109 | .. tip:: |
| 3110 | |
| 3111 | While the :meth:`_orm.Session.refresh` method is capable of |
| 3112 | refreshing both column and relationship oriented attributes, its |
| 3113 | primary focus is on refreshing of local column-oriented attributes |
| 3114 | on a single instance. For more open ended "refresh" functionality, |
| 3115 | including the ability to refresh the attributes on many objects at |
| 3116 | once while having explicit control over relationship loader |
| 3117 | strategies, use the |
| 3118 | :ref:`populate existing <orm_queryguide_populate_existing>` feature |
| 3119 | instead. |
| 3120 | |
| 3121 | Note that a highly isolated transaction will return the same values as |
| 3122 | were previously read in that same transaction, regardless of changes |
| 3123 | in database state outside of that transaction. Refreshing |
| 3124 | attributes usually only makes sense at the start of a transaction |
| 3125 | where database rows have not yet been accessed. |
| 3126 | |
| 3127 | :param attribute_names: optional. An iterable collection of |
| 3128 | string attribute names indicating a subset of attributes to |
| 3129 | be refreshed. |
| 3130 | |
| 3131 | :param with_for_update: optional boolean ``True`` indicating FOR UPDATE |
| 3132 | should be used, or may be a dictionary containing flags to |