Update joined table via SQLAlchemy ORM using session.query

ValAyal

Working with MySQL, I'd like to generate this SQL:

UPDATE tableA
INNER JOIN tableB
ON tableA.some_id = tableB.some_id
SET tableA.foo = 1
WHERE tableB.bar IN ('baz','baaz')

This is my SQLAlchemy query:

session.query(tableA).join(tableB, tableA.some_id == tableB.some_id) \
                     .filter(tableB.bar.in_(['baz','baaz']))\
                     .update({tableA.foo: 1})

But the SQL it generates is this (a multi-table update, with no join condition, which is not what I want):

UPDATE tableA, tableB 
SET tableA.foo = 1
WHERE tableB.bar IN ('baz','baaz')

I've tried changing the .join into another .filter to specify the join condition, that didn't solve the problem. How do I force this simple update statement to do the proper join?

Myles

As of version 0.7.4 sqlalchemy.sql.expression.update does allow you to refer to multiple tables in the WHERE clause. With this, you could build and execute an expression like:

users.update().values(name='ed').where(
        users.c.name==select([addresses.c.email_address]).\
                    where(addresses.c.user_id==users.c.id).\
                    as_scalar()
        )

(example straight from the link above)

The problem ValAyal is having is actually because Query.join() is not supported with Query.update(). Unfortunately, until 0.9.1 this was silently generating queries like the one ValAyal shared above. The changelog notes for 0.9.1 notes that the behavior was modified to emit a warning:

[orm] [bug] Query doesn’t support joins, subselects, or special FROM clauses when using the Query.update() or Query.delete() methods; instead of silently ignoring these fields if methods like Query.join() or Query.select_from() has been called, a warning is emitted. As of 1.0.0b5 this will raise an error.

References: #3349

We actually ran into this where I work just this evening and found that our code is, in fact, emitting the following warning (which says it will an error in 1.0):

SAWarning: Can't call Query.update() or Query.delete() when join(), outerjoin(), select_from(), or from_self() has been called.  This will be an exception in 1.0
  self._validate_query_state()

In our case, we opted to convert the update into a select and an update to one table.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update an entire single column with table joined query

From Dev

How to update using SQLAlchemy and session?

From Dev

Using a query on a joined table instead of actual table

From Dev

MySQL update query on a single table with value from joined table

From Dev

Load only subset of joined rows in SQLAlchemy ORM

From Dev

How to retrieve min price via mysql query from a joined table

From Dev

Delphi update a joined query

From Dev

SQLAlchemy ORM update value by checking if other table value in list

From Dev

UPDATE query with inner joined query

From Dev

UPDATE query with inner joined query

From Dev

Explain Keyed Tuple output from SQLAlchemy ORM Query using Aliased

From Dev

Explain Keyed Tuple output from SQLAlchemy ORM Query using Aliased

From Dev

SQLAlchemy: Using delete/update with a join query

From Dev

Where condition for joined table in Sequelize ORM

From Dev

Using DISTINCT on joined table

From Dev

constructing a dataframe from joined SQLAlchemy query

From Dev

filter sqlalchemy joined query, constructed on parsed input

From Dev

SQLalchemy Joined Inheritance Query Based on Mixin Column

From Dev

filter sqlalchemy joined query, constructed on parsed input

From Dev

constructing a dataframe from joined SQLAlchemy query

From Dev

Session Issue in my Joined Table for my Login

From Dev

How to update values using the ORM instead of a raw SQL query?

From Dev

Using data from a joined query

From Dev

SQLAlchemy join a "one to many table" and then filter on the joined table with a set of values

From Dev

sqlite: update TableA with a max() from a joined table

From Java

Sql update table with joined sums of other tables

From Dev

Is there a way to use a table variable as the source for a joined update?

From Dev

Update a table inner joined with itself in MySQL

From Dev

Update one table when joined to another

Related Related

  1. 1

    Update an entire single column with table joined query

  2. 2

    How to update using SQLAlchemy and session?

  3. 3

    Using a query on a joined table instead of actual table

  4. 4

    MySQL update query on a single table with value from joined table

  5. 5

    Load only subset of joined rows in SQLAlchemy ORM

  6. 6

    How to retrieve min price via mysql query from a joined table

  7. 7

    Delphi update a joined query

  8. 8

    SQLAlchemy ORM update value by checking if other table value in list

  9. 9

    UPDATE query with inner joined query

  10. 10

    UPDATE query with inner joined query

  11. 11

    Explain Keyed Tuple output from SQLAlchemy ORM Query using Aliased

  12. 12

    Explain Keyed Tuple output from SQLAlchemy ORM Query using Aliased

  13. 13

    SQLAlchemy: Using delete/update with a join query

  14. 14

    Where condition for joined table in Sequelize ORM

  15. 15

    Using DISTINCT on joined table

  16. 16

    constructing a dataframe from joined SQLAlchemy query

  17. 17

    filter sqlalchemy joined query, constructed on parsed input

  18. 18

    SQLalchemy Joined Inheritance Query Based on Mixin Column

  19. 19

    filter sqlalchemy joined query, constructed on parsed input

  20. 20

    constructing a dataframe from joined SQLAlchemy query

  21. 21

    Session Issue in my Joined Table for my Login

  22. 22

    How to update values using the ORM instead of a raw SQL query?

  23. 23

    Using data from a joined query

  24. 24

    SQLAlchemy join a "one to many table" and then filter on the joined table with a set of values

  25. 25

    sqlite: update TableA with a max() from a joined table

  26. 26

    Sql update table with joined sums of other tables

  27. 27

    Is there a way to use a table variable as the source for a joined update?

  28. 28

    Update a table inner joined with itself in MySQL

  29. 29

    Update one table when joined to another

HotTag

Archive