MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / before_update

Method before_update

lib/sqlalchemy/orm/events.py:1272–1343  ·  view source on GitHub ↗

Receive an object instance before an UPDATE statement is emitted corresponding to that instance. .. note:: this event **only** applies to the :ref:`session flush operation <session_flushing>` and does **not** apply to the ORM DML operations described at

(
        self, mapper: Mapper[_O], connection: Connection, target: _O
    )

Source from the content-addressed store, hash-verified

1270 """
1271
1272 def before_update(
1273 self, mapper: Mapper[_O], connection: Connection, target: _O
1274 ) -> None:
1275 """Receive an object instance before an UPDATE statement
1276 is emitted corresponding to that instance.
1277
1278 .. note:: this event **only** applies to the
1279 :ref:`session flush operation <session_flushing>`
1280 and does **not** apply to the ORM DML operations described at
1281 :ref:`orm_expression_update_delete`. To intercept ORM
1282 DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
1283
1284 This event is used to modify local, non-object related
1285 attributes on the instance before an UPDATE occurs, as well
1286 as to emit additional SQL statements on the given
1287 connection.
1288
1289 This method is called for all instances that are
1290 marked as "dirty", *even those which have no net changes
1291 to their column-based attributes*. An object is marked
1292 as dirty when any of its column-based attributes have a
1293 "set attribute" operation called or when any of its
1294 collections are modified. If, at update time, no
1295 column-based attributes have any net changes, no UPDATE
1296 statement will be issued. This means that an instance
1297 being sent to :meth:`~.MapperEvents.before_update` is
1298 *not* a guarantee that an UPDATE statement will be
1299 issued, although you can affect the outcome here by
1300 modifying attributes so that a net change in value does
1301 exist.
1302
1303 To detect if the column-based attributes on the object have net
1304 changes, and will therefore generate an UPDATE statement, use
1305 ``object_session(instance).is_modified(instance,
1306 include_collections=False)``.
1307
1308 The event is often called for a batch of objects of the
1309 same class before their UPDATE statements are emitted at
1310 once in a later step. In the extremely rare case that
1311 this is not desirable, the :class:`_orm.Mapper` can be
1312 configured with ``batch=False``, which will cause
1313 batches of instances to be broken up into individual
1314 (and more poorly performing) event->persist->event
1315 steps.
1316
1317 .. warning::
1318
1319 Mapper-level flush events only allow **very limited operations**,
1320 on attributes local to the row being operated upon only,
1321 as well as allowing any SQL to be emitted on the given
1322 :class:`_engine.Connection`. **Please read fully** the notes
1323 at :ref:`session_persistence_mapper` for guidelines on using
1324 these methods; generally, the :meth:`.SessionEvents.before_flush`
1325 method should be preferred for general on-flush changes.
1326
1327 :param mapper: the :class:`_orm.Mapper` which is the target
1328 of this event.
1329 :param connection: the :class:`_engine.Connection` being used to

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected