Python's SQLAlchemy doesn't clean out the secondary (many-to-many) table?

前端 未结 2 993
傲寒
傲寒 2021-02-20 12:20

I have a many-to-many relationship between Users and Tasks. I want the \"secondary table\" (meaning, the table that facilitates the many-to-many relat

2条回答
  •  日久生厌
    2021-02-20 13:12

    See delete(synchronize_session='evaluate'):

    The method does not offer in-Python cascading of relationships - it is assumed that ON DELETE CASCADE is configured for any foreign key references which require it. The Session needs to be expired (occurs automatically after commit(), or call expire_all()) in order for the state of dependent objects subject to delete or delete-orphan cascade to be correctly represented.

    That is, SQLAlchemy isn't able to find all the Task objects you're deleting and figure out each row to be deleted from user_tasks - the best way to do this is to use ON DELETE CASCADE on the foreign keys (won't work with MySQL MyISAM tables or SQLite if foreign keys aren't enabled):

    http://docs.sqlalchemy.org/en/latest/core/constraints.html#on-update-and-on-delete

提交回复
热议问题