return a new :class:`_query.Query` with the specified options for the ``FOR UPDATE`` clause. The behavior of this method is identical to that of :meth:`_expression.GenerativeSelect.with_for_update`. When called with no arguments, the resulting ``SELEC
(
self,
*,
nowait: bool = False,
read: bool = False,
of: Optional[_ForUpdateOfArgument] = None,
skip_locked: bool = False,
key_share: bool = False,
)
| 1793 | |
| 1794 | @_generative |
| 1795 | def with_for_update( |
| 1796 | self, |
| 1797 | *, |
| 1798 | nowait: bool = False, |
| 1799 | read: bool = False, |
| 1800 | of: Optional[_ForUpdateOfArgument] = None, |
| 1801 | skip_locked: bool = False, |
| 1802 | key_share: bool = False, |
| 1803 | ) -> Self: |
| 1804 | """return a new :class:`_query.Query` |
| 1805 | with the specified options for the |
| 1806 | ``FOR UPDATE`` clause. |
| 1807 | |
| 1808 | The behavior of this method is identical to that of |
| 1809 | :meth:`_expression.GenerativeSelect.with_for_update`. |
| 1810 | When called with no arguments, |
| 1811 | the resulting ``SELECT`` statement will have a ``FOR UPDATE`` clause |
| 1812 | appended. When additional arguments are specified, backend-specific |
| 1813 | options such as ``FOR UPDATE NOWAIT`` or ``LOCK IN SHARE MODE`` |
| 1814 | can take effect. |
| 1815 | |
| 1816 | E.g.:: |
| 1817 | |
| 1818 | q = ( |
| 1819 | sess.query(User) |
| 1820 | .populate_existing() |
| 1821 | .with_for_update(nowait=True, of=User) |
| 1822 | ) |
| 1823 | |
| 1824 | The above query on a PostgreSQL backend will render like: |
| 1825 | |
| 1826 | .. sourcecode:: sql |
| 1827 | |
| 1828 | SELECT users.id AS users_id FROM users FOR UPDATE OF users NOWAIT |
| 1829 | |
| 1830 | .. warning:: |
| 1831 | |
| 1832 | Using ``with_for_update`` in the context of eager loading |
| 1833 | relationships is not officially supported or recommended by |
| 1834 | SQLAlchemy and may not work with certain queries on various |
| 1835 | database backends. When ``with_for_update`` is successfully used |
| 1836 | with a query that involves :func:`_orm.joinedload`, SQLAlchemy will |
| 1837 | attempt to emit SQL that locks all involved tables. |
| 1838 | |
| 1839 | .. note:: It is generally a good idea to combine the use of the |
| 1840 | :meth:`_orm.Query.populate_existing` method when using the |
| 1841 | :meth:`_orm.Query.with_for_update` method. The purpose of |
| 1842 | :meth:`_orm.Query.populate_existing` is to force all the data read |
| 1843 | from the SELECT to be populated into the ORM objects returned, |
| 1844 | even if these objects are already in the :term:`identity map`. |
| 1845 | |
| 1846 | .. seealso:: |
| 1847 | |
| 1848 | :meth:`_expression.GenerativeSelect.with_for_update` |
| 1849 | - Core level method with |
| 1850 | full argument and behavioral description. |
| 1851 | |
| 1852 | :meth:`_orm.Query.populate_existing` - overwrites attributes of |