Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy: update from_select

I need to execute query like

UPDATE node
SET node.parent_id = node_node.parent_id,
    node.label = node_node.label
FROM node_node
WHERE node_node.child_id = node_id

using SQLAlchemy. I did search the docs, and found only insert().from_select(), but no update().from_select(). I know I can achieve the same programatically, but I need it to be as fast as possible.

Is it possible? Could you give me an example or link to docs/any clue?

like image 613
Bartosz Marcinkowski Avatar asked Apr 12 '14 12:04

Bartosz Marcinkowski


1 Answers

Assuming that t_node is node Table instance, while t_node_node - node_node Table instance, see the statement below.

upd = (t_node.update()
        .values(
            parent_id = t_node_node.c.parent_id,
            label = t_node_node.c.label,
            )
        .where(t_node_node.c.child_id == t_node.c.node_id)
        )

Read more on Inserts, Updates and Deletes documentation for more information.

like image 77
van Avatar answered Oct 01 '22 17:10

van