MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / before_insert

Method before_insert

lib/sqlalchemy/orm/events.py:1164–1216  ·  view source on GitHub ↗

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

1162 """
1163
1164 def before_insert(
1165 self, mapper: Mapper[_O], connection: Connection, target: _O
1166 ) -> None:
1167 """Receive an object instance before an INSERT statement
1168 is emitted corresponding to that instance.
1169
1170 .. note:: this event **only** applies to the
1171 :ref:`session flush operation <session_flushing>`
1172 and does **not** apply to the ORM DML operations described at
1173 :ref:`orm_expression_update_delete`. To intercept ORM
1174 DML events, use :meth:`_orm.SessionEvents.do_orm_execute`.
1175
1176 This event is used to modify local, non-object related
1177 attributes on the instance before an INSERT occurs, as well
1178 as to emit additional SQL statements on the given
1179 connection.
1180
1181 The event is often called for a batch of objects of the
1182 same class before their INSERT statements are emitted at
1183 once in a later step. In the extremely rare case that
1184 this is not desirable, the :class:`_orm.Mapper` object can be
1185 configured with ``batch=False``, which will cause
1186 batches of instances to be broken up into individual
1187 (and more poorly performing) event->persist->event
1188 steps.
1189
1190 .. warning::
1191
1192 Mapper-level flush events only allow **very limited operations**,
1193 on attributes local to the row being operated upon only,
1194 as well as allowing any SQL to be emitted on the given
1195 :class:`_engine.Connection`. **Please read fully** the notes
1196 at :ref:`session_persistence_mapper` for guidelines on using
1197 these methods; generally, the :meth:`.SessionEvents.before_flush`
1198 method should be preferred for general on-flush changes.
1199
1200 :param mapper: the :class:`_orm.Mapper` which is the target
1201 of this event.
1202 :param connection: the :class:`_engine.Connection` being used to
1203 emit INSERT statements for this instance. This
1204 provides a handle into the current transaction on the
1205 target database specific to this instance.
1206 :param target: the mapped instance being persisted. If
1207 the event is configured with ``raw=True``, this will
1208 instead be the :class:`.InstanceState` state-management
1209 object associated with the instance.
1210 :return: No return value is supported by this event.
1211
1212 .. seealso::
1213
1214 :ref:`session_persistence_events`
1215
1216 """
1217
1218 def after_insert(
1219 self, mapper: Mapper[_O], connection: Connection, target: _O

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected