Perform a bulk save of the given list of objects. .. legacy:: This method is a legacy feature as of the 2.0 series of SQLAlchemy. For modern bulk INSERT and UPDATE, see the sections :ref:`orm_queryguide_bulk_insert` and :ref:`orm_queryguide
(
self,
objects: Iterable[object],
return_defaults: bool = False,
update_changed_only: bool = True,
preserve_order: bool = True,
)
| 4489 | transaction.rollback(_capture_exception=True) |
| 4490 | |
| 4491 | def bulk_save_objects( |
| 4492 | self, |
| 4493 | objects: Iterable[object], |
| 4494 | return_defaults: bool = False, |
| 4495 | update_changed_only: bool = True, |
| 4496 | preserve_order: bool = True, |
| 4497 | ) -> None: |
| 4498 | """Perform a bulk save of the given list of objects. |
| 4499 | |
| 4500 | .. legacy:: |
| 4501 | |
| 4502 | This method is a legacy feature as of the 2.0 series of |
| 4503 | SQLAlchemy. For modern bulk INSERT and UPDATE, see |
| 4504 | the sections :ref:`orm_queryguide_bulk_insert` and |
| 4505 | :ref:`orm_queryguide_bulk_update`. |
| 4506 | |
| 4507 | For general INSERT and UPDATE of existing ORM mapped objects, |
| 4508 | prefer standard :term:`unit of work` data management patterns, |
| 4509 | introduced in the :ref:`unified_tutorial` at |
| 4510 | :ref:`tutorial_orm_data_manipulation`. SQLAlchemy 2.0 |
| 4511 | now uses :ref:`engine_insertmanyvalues` with modern dialects |
| 4512 | which solves previous issues of bulk INSERT slowness. |
| 4513 | |
| 4514 | :param objects: a sequence of mapped object instances. The mapped |
| 4515 | objects are persisted as is, and are **not** associated with the |
| 4516 | :class:`.Session` afterwards. |
| 4517 | |
| 4518 | For each object, whether the object is sent as an INSERT or an |
| 4519 | UPDATE is dependent on the same rules used by the :class:`.Session` |
| 4520 | in traditional operation; if the object has the |
| 4521 | :attr:`.InstanceState.key` |
| 4522 | attribute set, then the object is assumed to be "detached" and |
| 4523 | will result in an UPDATE. Otherwise, an INSERT is used. |
| 4524 | |
| 4525 | In the case of an UPDATE, statements are grouped based on which |
| 4526 | attributes have changed, and are thus to be the subject of each |
| 4527 | SET clause. If ``update_changed_only`` is False, then all |
| 4528 | attributes present within each object are applied to the UPDATE |
| 4529 | statement, which may help in allowing the statements to be grouped |
| 4530 | together into a larger executemany(), and will also reduce the |
| 4531 | overhead of checking history on attributes. |
| 4532 | |
| 4533 | :param return_defaults: when True, rows that are missing values which |
| 4534 | generate defaults, namely integer primary key defaults and sequences, |
| 4535 | will be inserted **one at a time**, so that the primary key value |
| 4536 | is available. In particular this will allow joined-inheritance |
| 4537 | and other multi-table mappings to insert correctly without the need |
| 4538 | to provide primary key values ahead of time; however, |
| 4539 | :paramref:`.Session.bulk_save_objects.return_defaults` **greatly |
| 4540 | reduces the performance gains** of the method overall. It is strongly |
| 4541 | advised to please use the standard :meth:`_orm.Session.add_all` |
| 4542 | approach. |
| 4543 | |
| 4544 | :param update_changed_only: when True, UPDATE statements are rendered |
| 4545 | based on those attributes in each state that have logged changes. |
| 4546 | When False, all attributes present are rendered into the SET clause |
| 4547 | with the exception of primary key attributes. |
| 4548 |