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,
)
| 4655 | transaction.rollback(_capture_exception=True) |
| 4656 | |
| 4657 | def bulk_save_objects( |
| 4658 | self, |
| 4659 | objects: Iterable[object], |
| 4660 | return_defaults: bool = False, |
| 4661 | update_changed_only: bool = True, |
| 4662 | preserve_order: bool = True, |
| 4663 | ) -> None: |
| 4664 | """Perform a bulk save of the given list of objects. |
| 4665 | |
| 4666 | .. legacy:: |
| 4667 | |
| 4668 | This method is a legacy feature as of the 2.0 series of |
| 4669 | SQLAlchemy. For modern bulk INSERT and UPDATE, see |
| 4670 | the sections :ref:`orm_queryguide_bulk_insert` and |
| 4671 | :ref:`orm_queryguide_bulk_update`. |
| 4672 | |
| 4673 | For general INSERT and UPDATE of existing ORM mapped objects, |
| 4674 | prefer standard :term:`unit of work` data management patterns, |
| 4675 | introduced in the :ref:`unified_tutorial` at |
| 4676 | :ref:`tutorial_orm_data_manipulation`. SQLAlchemy 2.0 |
| 4677 | now uses :ref:`engine_insertmanyvalues` with modern dialects |
| 4678 | which solves previous issues of bulk INSERT slowness. |
| 4679 | |
| 4680 | :param objects: a sequence of mapped object instances. The mapped |
| 4681 | objects are persisted as is, and are **not** associated with the |
| 4682 | :class:`.Session` afterwards. |
| 4683 | |
| 4684 | For each object, whether the object is sent as an INSERT or an |
| 4685 | UPDATE is dependent on the same rules used by the :class:`.Session` |
| 4686 | in traditional operation; if the object has the |
| 4687 | :attr:`.InstanceState.key` |
| 4688 | attribute set, then the object is assumed to be "detached" and |
| 4689 | will result in an UPDATE. Otherwise, an INSERT is used. |
| 4690 | |
| 4691 | In the case of an UPDATE, statements are grouped based on which |
| 4692 | attributes have changed, and are thus to be the subject of each |
| 4693 | SET clause. If ``update_changed_only`` is False, then all |
| 4694 | attributes present within each object are applied to the UPDATE |
| 4695 | statement, which may help in allowing the statements to be grouped |
| 4696 | together into a larger executemany(), and will also reduce the |
| 4697 | overhead of checking history on attributes. |
| 4698 | |
| 4699 | :param return_defaults: when True, rows that are missing values which |
| 4700 | generate defaults, namely integer primary key defaults and sequences, |
| 4701 | will be inserted **one at a time**, so that the primary key value |
| 4702 | is available. In particular this will allow joined-inheritance |
| 4703 | and other multi-table mappings to insert correctly without the need |
| 4704 | to provide primary key values ahead of time; however, |
| 4705 | :paramref:`.Session.bulk_save_objects.return_defaults` **greatly |
| 4706 | reduces the performance gains** of the method overall. It is strongly |
| 4707 | advised to please use the standard :meth:`_orm.Session.add_all` |
| 4708 | approach. |
| 4709 | |
| 4710 | :param update_changed_only: when True, UPDATE statements are rendered |
| 4711 | based on those attributes in each state that have logged changes. |
| 4712 | When False, all attributes present are rendered into the SET clause |
| 4713 | with the exception of primary key attributes. |
| 4714 |