MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / with_for_update

Method with_for_update

lib/sqlalchemy/orm/query.py:1775–1844  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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

Callers 15

test_for_updateMethod · 0.45
CoreFixturesClass · 0.45
test_basic_cloneMethod · 0.45
test_adaptMethod · 0.45
id_generatorMethod · 0.45
_assertMethod · 0.45
test_join_w_subqueryMethod · 0.45

Calls 1

ForUpdateArgClass · 0.85

Tested by 15

test_for_updateMethod · 0.36
test_basic_cloneMethod · 0.36
test_adaptMethod · 0.36
id_generatorMethod · 0.36
_assertMethod · 0.36
test_join_w_subqueryMethod · 0.36
test_plainMethod · 0.36
test_default_updateMethod · 0.36