On 05/04/2017 08:41 AM, yoch.me...@gmail.com wrote:
Hi,

I'm facing to a strange behavior with bulk update on inherited class.

These two queries work differently :

# raise : Unconsumed column names: name
try:
session.query(Engineer).filter(Engineer.name=='bar').update({'name':'baz'})
         session.commit()
exceptExceptionaserr:
print(err)

# with MySQL engine , produce : 'UPDATE engineer, person SET person.name=%s WHERE person.name = %s' # with SQLite engine, produce : 'UPDATE engineer SET name=? FROM person WHERE person.name = ?' [Syntax Error]
try:
session.query(Engineer).filter(Engineer.status=='working').update({'name':'bar'})
         session.commit()
exceptExceptionaserr:
print(err)
|

The former query fails, presumably because sqlalchemy don't care with the parent class. The later is handled correctly with MySQL (maybe because the fillter involve Person), but no with SQLite.

(I also tried with_polymorphic, but it doesn't solve the problem).

Is this a bug ?

it's not.

http://docs.sqlalchemy.org/en/latest/orm/query.html?highlight=query%20update#sqlalchemy.orm.query.Query.update



**Warning**

The Query.update() method is a “bulk” operation, which bypasses ORM unit-of-work automation in favor of greater performance. **Please read all caveats and warnings below.**

... below ....


* The method supports multiple table updates, as detailed in Multiple Table Updates, and this behavior does extend to support updates of joined-inheritance and other multiple table mappings. However, the join condition of an inheritance mapper is not automatically rendered. Care must be taken in any multiple-table update to explicitly include the joining condition between those tables, even in mappings where this is normally automatic. E.g. if a class Engineer subclasses Employee, an UPDATE of the Engineer local table using criteria against the Employee local table might look like:

session.query(Engineer).\
    filter(Engineer.id == Employee.id).\
    filter(Employee.name == 'dilbert').\
    update({"engineer_type": "programmer"})


In your case, your query is only targeting columns in the base "person" table. So this is not really a multiple table update and instead of asking it for query(Engineer) you should be asking for query(Person).filter(Person.type == 'engineer').





Best regards,
yoch

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to