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

Method after_insert

lib/sqlalchemy/orm/events.py:1225–1277  ·  view source on GitHub ↗

Receive an object instance after an INSERT 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

1223 """
1224
1225 def after_insert(
1226 self, mapper: Mapper[_O], connection: Connection, target: _O
1227 ) -> None:
1228 """Receive an object instance after an INSERT statement
1229 is emitted corresponding to that instance.
1230
1231 .. note:: this event **only** applies to the
1232 :ref:`session flush operation <session_flushing>`
1233 and does **not** apply to the ORM DML operations described at
1234 :ref:`orm_expression_update_delete`. To intercept ORM
1235 DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
1236
1237 This event is used to modify in-Python-only
1238 state on the instance after an INSERT occurs, as well
1239 as to emit additional SQL statements on the given
1240 connection.
1241
1242 The event is often called for a batch of objects of the
1243 same class after their INSERT statements have been
1244 emitted at once in a previous step. In the extremely
1245 rare case that this is not desirable, the
1246 :class:`_orm.Mapper` object can be configured with ``batch=False``,
1247 which will cause batches of instances to be broken up
1248 into individual (and more poorly performing)
1249 event->persist->event steps.
1250
1251 .. warning::
1252
1253 Mapper-level flush events only allow **very limited operations**,
1254 on attributes local to the row being operated upon only,
1255 as well as allowing any SQL to be emitted on the given
1256 :class:`_engine.Connection`. **Please read fully** the notes
1257 at :ref:`session_persistence_mapper` for guidelines on using
1258 these methods; generally, the :meth:`.SessionEvents.before_flush`
1259 method should be preferred for general on-flush changes.
1260
1261 :param mapper: the :class:`_orm.Mapper` which is the target
1262 of this event.
1263 :param connection: the :class:`_engine.Connection` being used to
1264 emit INSERT statements for this instance. This
1265 provides a handle into the current transaction on the
1266 target database specific to this instance.
1267 :param target: the mapped instance being persisted. If
1268 the event is configured with ``raw=True``, this will
1269 instead be the :class:`.InstanceState` state-management
1270 object associated with the instance.
1271 :return: No return value is supported by this event.
1272
1273 .. seealso::
1274
1275 :ref:`session_persistence_events`
1276
1277 """
1278
1279 def before_update(
1280 self, mapper: Mapper[_O], connection: Connection, target: _O

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected