I want to update multiple columns of one table according to other multiple columns of another table in SQLAlchemy. I'm using SQLite when testing it, so I can't use the `UPDATE table1 SET col=val WHERE table1.key == table2.key" syntax.
In other words, I'm trying to create this sort of update query:
UPDATE table1
SET
col1 = (SELECT col1 FROM table2 WHERE table2.key == table1.key),
col2 = (SELECT col2 FROM table2 WHERE table2.key == table1.key)
In SQLAlchemy:
select_query1 = select([table2.c.col1]).where(table1.c.key == table2.c.key)
select_query2 = select([table2.c.col2]).where(table1.c.key == table2.c.key)
session.execute(table.update().values(col1=select_query1, col2=select_query2))
Only I'd like to do the query only once instead of twice, unless SQLite and MySQL are smart enough not to make that query twice themselves.