Make use of a :term:`RETURNING` clause for the purpose of fetching server-side expressions and defaults, for supporting backends only. .. deepalchemy:: The :meth:`.UpdateBase.return_defaults` method is used by the ORM for its internal work in fetchin
(
self,
*cols: _DMLColumnArgument,
supplemental_cols: Optional[Iterable[_DMLColumnArgument]] = None,
sort_by_parameter_order: bool = False,
)
| 498 | |
| 499 | @_generative |
| 500 | def return_defaults( |
| 501 | self, |
| 502 | *cols: _DMLColumnArgument, |
| 503 | supplemental_cols: Optional[Iterable[_DMLColumnArgument]] = None, |
| 504 | sort_by_parameter_order: bool = False, |
| 505 | ) -> Self: |
| 506 | """Make use of a :term:`RETURNING` clause for the purpose |
| 507 | of fetching server-side expressions and defaults, for supporting |
| 508 | backends only. |
| 509 | |
| 510 | .. deepalchemy:: |
| 511 | |
| 512 | The :meth:`.UpdateBase.return_defaults` method is used by the ORM |
| 513 | for its internal work in fetching newly generated primary key |
| 514 | and server default values, in particular to provide the underlying |
| 515 | implementation of the :paramref:`_orm.Mapper.eager_defaults` |
| 516 | ORM feature as well as to allow RETURNING support with bulk |
| 517 | ORM inserts. Its behavior is fairly idiosyncratic |
| 518 | and is not really intended for general use. End users should |
| 519 | stick with using :meth:`.UpdateBase.returning` in order to |
| 520 | add RETURNING clauses to their INSERT, UPDATE and DELETE |
| 521 | statements. |
| 522 | |
| 523 | Normally, a single row INSERT statement will automatically populate the |
| 524 | :attr:`.CursorResult.inserted_primary_key` attribute when executed, |
| 525 | which stores the primary key of the row that was just inserted in the |
| 526 | form of a :class:`.Row` object with column names as named tuple keys |
| 527 | (and the :attr:`.Row._mapping` view fully populated as well). The |
| 528 | dialect in use chooses the strategy to use in order to populate this |
| 529 | data; if it was generated using server-side defaults and / or SQL |
| 530 | expressions, dialect-specific approaches such as ``cursor.lastrowid`` |
| 531 | or ``RETURNING`` are typically used to acquire the new primary key |
| 532 | value. |
| 533 | |
| 534 | However, when the statement is modified by calling |
| 535 | :meth:`.UpdateBase.return_defaults` before executing the statement, |
| 536 | additional behaviors take place **only** for backends that support |
| 537 | RETURNING and for :class:`.Table` objects that maintain the |
| 538 | :paramref:`.Table.implicit_returning` parameter at its default value of |
| 539 | ``True``. In these cases, when the :class:`.CursorResult` is returned |
| 540 | from the statement's execution, not only will |
| 541 | :attr:`.CursorResult.inserted_primary_key` be populated as always, the |
| 542 | :attr:`.CursorResult.returned_defaults` attribute will also be |
| 543 | populated with a :class:`.Row` named-tuple representing the full range |
| 544 | of server generated |
| 545 | values from that single row, including values for any columns that |
| 546 | specify :paramref:`_schema.Column.server_default` or which make use of |
| 547 | :paramref:`_schema.Column.default` using a SQL expression. |
| 548 | |
| 549 | When invoking INSERT statements with multiple rows using |
| 550 | :ref:`insertmanyvalues <engine_insertmanyvalues>`, the |
| 551 | :meth:`.UpdateBase.return_defaults` modifier will have the effect of |
| 552 | the :attr:`_engine.CursorResult.inserted_primary_key_rows` and |
| 553 | :attr:`_engine.CursorResult.returned_defaults_rows` attributes being |
| 554 | fully populated with lists of :class:`.Row` objects representing newly |
| 555 | inserted primary key values as well as newly inserted server generated |
| 556 | values for each row inserted. The |
| 557 | :attr:`.CursorResult.inserted_primary_key` and |