This question has been asked multiple times on stackoverflow, but they all appeared to be over a year old so I figured I would ask again in case there has been an update.
A correlated update
is an update statement that updates all rows in one table based on values from another table, while linking the two tables together.
From the SQLAlchemy docs, we can do a correlated update easily but only on a single column:
update(foo).values(bar=select([foobar.c.bar]).where(foobar.c.id == foo.c.id))
This translates into:
UPDATE foo
SET bar = (
SELECT bar
FROM foobar
WHERE foobar.id = foo.id
)
How can we write a correlated update using more than one column in sqlalchemy? For example:
UPDATE foo
SET (bar, baz) = (
SELECT bar, baz
FROM foobar
WHERE foobar.id = foo.id
)