include single table inh discriminator for query.update() / query.delete()?

Issue #3903 new
Ollie Rutherfurd
created an issue

ie session.query(Subclass).update(...) updates all rows in the table vs just Subclass rows.

Script attached that demonstrates the issue. There are two rows, only one should be updated, but both are updated.

Comments (4)

  1. Michael Bayer repo owner

    just had #3902 earlier today....again, this is a documented limitation:

    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. The polymorphic identity WHERE criteria is not included for single- or joined- table updates - this must be added manually, even for single table inheritance.

    I suspect this may be a backwards compatibility issue and I'm pretty sure this could be added as an enhancement but for the moment it's not part of the usage contract and it's documented.

  2. Ollie Rutherfurd reporter

    Ok, thanks -- definitely missed it in the docs. It would be great if this were added. It was a couple weeks ago when I ran into this, and I can't remember if I was running an update or delete, but I have a feeling it was a delete and almost accidentally emptied the table.

  3. Michael Bayer repo owner

    there may have been reasons other than backwards compat that this is not present. at the very least, we'd probably need to get #1653 in as the single-inh discriminator uses IN. One aspect here is that since we don't do the "joined" inheritance setup, we omit the single as well since the "spirit" of query.update() / query.delete() is that inheritance situations require special attention; having the single inh criteria be automatic here would conflict with that.

  4. Michael Bayer repo owner
    • changed milestone to 1.4

    this is definitely not in 1.3, and im not really sure what to do on this one. i think to handle the joined/single thing we'd need to use the discriminator in both cases. Also #1653 should not be hard to do, do that at the same time.

  5. Log in to comment