--- /dev/null
+.. change::
+ :tags: bug, sql
+ :tickets: 10408
+
+ Fixed issue where referring to a FROM entry in the SET clause of an UPDATE
+ statement would not include it in the FROM clause of the UPDATE statement,
+ if that entry were nowhere else in the statement; this occurs currently for
+ CTEs that were added using :meth:`.Update.add_cte` to provide the desired
+ CTE at the top of the statement.
primary_table = all_tables[0]
seen = {primary_table}
- for crit in statement._where_criteria:
+ consider = statement._where_criteria
+ if self._dict_parameters:
+ consider += tuple(self._dict_parameters.values())
+
+ for crit in consider:
for item in _from_objects(crit):
if not seen.intersection(item._cloned_set):
froms.append(item)
from sqlalchemy.sql import cte
from sqlalchemy.sql import exists
from sqlalchemy.sql import func
+from sqlalchemy.sql import insert
from sqlalchemy.sql import literal
from sqlalchemy.sql import select
from sqlalchemy.sql import table
dialect=dialect,
)
+ def test_insert_update_w_add_cte(self):
+ """test #10408"""
+ a = table(
+ "a", column("id"), column("x"), column("y"), column("next_id")
+ )
+
+ insert_a_cte = (insert(a).values(x=10, y=15).returning(a.c.id)).cte(
+ "insert_a_cte"
+ )
+
+ update_query = (
+ update(a)
+ .values(next_id=insert_a_cte.c.id)
+ .where(a.c.id == 10)
+ .add_cte(insert_a_cte)
+ )
+
+ self.assert_compile(
+ update_query,
+ "WITH insert_a_cte AS (INSERT INTO a (x, y) "
+ "VALUES (:param_1, :param_2) RETURNING a.id) "
+ "UPDATE a SET next_id=insert_a_cte.id "
+ "FROM insert_a_cte WHERE a.id = :id_1",
+ )
+
def test_anon_update_cte(self):
orders = table("orders", column("region"))
stmt = (