MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / after_update

Method after_update

lib/sqlalchemy/orm/events.py:1352–1422  ·  view source on GitHub ↗

Receive an object instance after an UPDATE statement is emitted corresponding to that instance. .. note:: this event **only** applies to the :ref:`session flush operation ` 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

1350 """
1351
1352 def after_update(
1353 self, mapper: Mapper[_O], connection: Connection, target: _O
1354 ) -> None:
1355 """Receive an object instance after an UPDATE statement
1356 is emitted corresponding to that instance.
1357
1358 .. note:: this event **only** applies to the
1359 :ref:`session flush operation <session_flushing>`
1360 and does **not** apply to the ORM DML operations described at
1361 :ref:`orm_expression_update_delete`. To intercept ORM
1362 DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
1363
1364 This event is used to modify in-Python-only
1365 state on the instance after an UPDATE occurs, as well
1366 as to emit additional SQL statements on the given
1367 connection.
1368
1369 This method is called for all instances that are
1370 marked as "dirty", *even those which have no net changes
1371 to their column-based attributes*, and for which
1372 no UPDATE statement has proceeded. An object is marked
1373 as dirty when any of its column-based attributes have a
1374 "set attribute" operation called or when any of its
1375 collections are modified. If, at update time, no
1376 column-based attributes have any net changes, no UPDATE
1377 statement will be issued. This means that an instance
1378 being sent to :meth:`~.MapperEvents.after_update` is
1379 *not* a guarantee that an UPDATE statement has been
1380 issued.
1381
1382 To detect if the column-based attributes on the object have net
1383 changes, and therefore resulted in an UPDATE statement, use
1384 ``object_session(instance).is_modified(instance,
1385 include_collections=False)``.
1386
1387 The event is often called for a batch of objects of the
1388 same class after their UPDATE statements have been emitted at
1389 once in a previous step. In the extremely rare case that
1390 this is not desirable, the :class:`_orm.Mapper` can be
1391 configured with ``batch=False``, which will cause
1392 batches of instances to be broken up into individual
1393 (and more poorly performing) event->persist->event
1394 steps.
1395
1396 .. warning::
1397
1398 Mapper-level flush events only allow **very limited operations**,
1399 on attributes local to the row being operated upon only,
1400 as well as allowing any SQL to be emitted on the given
1401 :class:`_engine.Connection`. **Please read fully** the notes
1402 at :ref:`session_persistence_mapper` for guidelines on using
1403 these methods; generally, the :meth:`.SessionEvents.before_flush`
1404 method should be preferred for general on-flush changes.
1405
1406 :param mapper: the :class:`_orm.Mapper` which is the target
1407 of this event.
1408 :param connection: the :class:`_engine.Connection` being used to
1409 emit UPDATE statements for this instance. This

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected