]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
add example to update from that uses values
authorFederico Caselli <cfederico87@gmail.com>
Tue, 10 Jun 2025 21:42:35 +0000 (23:42 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 24 Jun 2025 21:43:43 +0000 (17:43 -0400)
Change-Id: Ic02a722be9a30851a87e0da4759c728e86fb22c8
References: #11768

doc/build/orm/declarative_tables.rst
doc/build/tutorial/data_update.rst
tools/format_docs_code.py

index 4102680b75ee41517bf5c839e0c89ceefb9ae07d..5eb9578568de2a50a7548e85cf5f739796e1e19f 100644 (file)
@@ -1026,9 +1026,9 @@ more open ended.
             col_a: Mapped[str | float | bool | None]
             col_b: Mapped[str | float | bool]
 
-    This raises an error since the union types used by ``col_a`` or ``col_b``,
-    are not found in ``TABase`` type map and ``JsonScalar`` must be referenced
-    directly.
+  This raises an error since the union types used by ``col_a`` or ``col_b``,
+  are not found in ``TABase`` type map and ``JsonScalar`` must be referenced
+  directly.
 
 .. _orm_declarative_mapped_column_pep593:
 
index e32b6676c76d0b72f3cfa108ebd95693348fe4ec..d21b153144d18ea603c18da8110f6f10265b59d5 100644 (file)
@@ -135,7 +135,7 @@ anywhere a column expression might be placed::
 UPDATE..FROM
 ~~~~~~~~~~~~~
 
-Some databases such as PostgreSQL and MySQL support a syntax "UPDATE FROM"
+Some databases such as PostgreSQL, MSSQL and MySQL support a syntax ``UPDATE...FROM``
 where additional tables may be stated directly in a special FROM clause. This
 syntax will be generated implicitly when additional tables are located in the
 WHERE clause of the statement::
@@ -172,6 +172,27 @@ order to refer to additional tables::
   SET address.email_address=%s, user_account.fullname=%s
   WHERE user_account.id = address.user_id AND address.email_address = %s
 
+``UPDATE...FROM`` can also be
+combined with the :class:`_sql.Values` construct
+on backends such as PostgreSQL, to create a single UPDATE statement that updates
+multiple rows at once against the named form of VALUES::
+
+  >>> from sqlalchemy import Values
+  >>> values = Values(
+  ...     user_table.c.id,
+  ...     user_table.c.name,
+  ...     name="my_values",
+  ... ).data([(1, "new_name"), (2, "another_name"), ("3", "name_name")])
+  >>> update_stmt = (
+  ...     user_table.update().values(name=values.c.name).where(user_table.c.id == values.c.id)
+  ... )
+  >>> from sqlalchemy.dialects import postgresql
+  >>> print(update_stmt.compile(dialect=postgresql.dialect()))
+  {printsql}UPDATE user_account
+  SET name=my_values.name
+  FROM (VALUES (%(param_1)s, %(param_2)s), (%(param_3)s, %(param_4)s), (%(param_5)s, %(param_6)s)) AS my_values (id, name)
+  WHERE user_account.id = my_values.id
+
 .. _tutorial_parameter_ordered_updates:
 
 Parameter Ordered Updates
index a3b6965c862a4a25137c4f604db2acb03192b625..76c341652e34816e31d85b70b2002d886f3f7fec 100644 (file)
@@ -31,7 +31,7 @@ ignore_paths = (
     re.compile(r"changelog/unreleased_\d{2}"),
     re.compile(r"README\.unittests\.rst"),
     re.compile(r"\.tox"),
-    re.compile(r"build"),
+    re.compile(rf"{home.as_posix()}/build"),
 )
 
 CUSTOM_TARGET_VERSIONS = {"declarative_tables.rst": "PY312"}