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

前端 未结 2 1544
抹茶落季
抹茶落季 2021-02-20 12:52

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:25

    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

提交回复
热议问题