Flush all the object changes to the database. Writes out all pending object creations, deletions and modifications to the database as INSERTs, DELETEs, UPDATEs, etc. Operations are automatically ordered by the Session's unit of work dependency solver. Datab
(self, objects: Optional[Sequence[Any]] = None)
| 4477 | return state in self._new or self.identity_map.contains_state(state) |
| 4478 | |
| 4479 | def flush(self, objects: Optional[Sequence[Any]] = None) -> None: |
| 4480 | """Flush all the object changes to the database. |
| 4481 | |
| 4482 | Writes out all pending object creations, deletions and modifications |
| 4483 | to the database as INSERTs, DELETEs, UPDATEs, etc. Operations are |
| 4484 | automatically ordered by the Session's unit of work dependency |
| 4485 | solver. |
| 4486 | |
| 4487 | Database operations will be issued in the current transactional |
| 4488 | context and do not affect the state of the transaction, unless an |
| 4489 | error occurs, in which case the entire transaction is rolled back. |
| 4490 | You may flush() as often as you like within a transaction to move |
| 4491 | changes from Python to the database's transaction buffer. |
| 4492 | |
| 4493 | :param objects: Optional; restricts the flush operation to operate |
| 4494 | only on elements that are in the given collection. |
| 4495 | |
| 4496 | This feature is for an extremely narrow set of use cases where |
| 4497 | particular objects may need to be operated upon before the |
| 4498 | full flush() occurs. It is not intended for general use. |
| 4499 | |
| 4500 | .. deprecated:: 2.1 |
| 4501 | |
| 4502 | """ |
| 4503 | |
| 4504 | if self._flushing: |
| 4505 | raise sa_exc.InvalidRequestError("Session is already flushing") |
| 4506 | |
| 4507 | if self._is_clean(): |
| 4508 | return |
| 4509 | try: |
| 4510 | self._flushing = True |
| 4511 | self._flush(objects) |
| 4512 | finally: |
| 4513 | self._flushing = False |
| 4514 | |
| 4515 | def _flush_warning(self, method: Any) -> None: |
| 4516 | util.warn( |