You haven't given a complete script to reproduce the problem, so I'm
not certain what happened. The following is just a guess

You are using query(...).delete() with joined-table inheritance, which
has a lot of caveats in the docs:

    
https://docs.sqlalchemy.org/en/13/orm/query.html#sqlalchemy.orm.query.Query.delete

As far as I can tell, SA will issue a query to delete rows from the
item table. You've got ON DELETE CASCADE on the item_meta.id foreign
key, so I guess the db will delete the corresponding rows from
item_meta.

The default value for synchronize_session in Query.delete is
'evaluate', which means SA will try to find objects in the in-memory
session which match your deletion criteria and remove them from the
session. I'm guessing that this step is missing at least one ItemMeta
object for some reason.

When you query for Item objects, SA will first autoflush any pending
changes in the session. It looks like you have some pending changes on
an ItemMeta instance, so it tries to flush those, but the
corresponding row has already been deleted, hence the error.

If you provide a runnable test script, we may be able to give more answers.

Hope that helps,

Simon

On Fri, Oct 11, 2019 at 10:08 AM natsjoo sodillepa <snats...@gmail.com> wrote:
>
> Hi all,
>
> We have a list of polymorphic objects from which delete object does not work, 
> not matter what we try.
> The situation:
>
> class ItemGroup(Base):
>     __tablename__ = 'item_group'
>     __table_args__ = (
>         UniqueConstraint('model_id', 'item_group_color_id', 
> name='unique_model_id_item_group_color_id_uc'),
>     )
>
>     id = Column(Integer, primary_key=True)
>     items = relationship("Item", back_populates="item_group")
>
> class Item(Base):
>     __tablename__ = 'item'
>
>     id = Column(Integer, primary_key=True)
>
>     item_group_id = Column(ForeignKey('item_group.id'), nullable=False, 
> index=True)
>     item_group = relationship('ItemGroup', back_populates="items", 
> uselist=False)
>
>     __mapper_args__ = {
>         'polymorphic_identity': __tablename__,
>         'polymorphic_on': item_type
>     }
>
> class ItemMeta(Item):
>     __tablename__ = 'item_meta'
>
>     id = Column(Integer, ForeignKey('item.id', ondelete="CASCADE"), 
> primary_key=True)
>
>     meta_name = Column(String(255, collation), nullable=False)
>
>     __mapper_args__ = {
>         'polymorphic_identity': __tablename__,
>     }
>
>
> The problem occurs after a delete:
>
>   session.query(ItemMeta).filter_by(item_group=ig).delete()
>
> Now, querying the child works fine:
>   session.query(ItemMeta).filter_by(item_group=ig).all()
>   []
>
> But querying the parent:
>   test_fixtures.session.query(Item).filter_by(item_group=ig).all()
>
>
>   Give: Instance '<ItemMeta at 0x7f0b024afcf8>' has been deleted, or its row 
> is otherwise not present.
>
> In the database I can see the lingering parent objects. I guess that I have 
> to use something like
> delete-orphan, but as I dont have a relation from child to father, so now I'm 
> stuck.
>
> Any ideas are welcome.
>
> Kind regards,
> Nacho
>
> --
> 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 view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/7dea2eaa-6390-4a54-abd5-fae925727c17%40googlegroups.com.

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexdiKj4vPGW%2BSB-HC6ZU%3DwABe6Vz98pSjZBNp6wLZ3DH1w%40mail.gmail.com.

Reply via email to