SQLAlchemy: cascade delete

后端 未结 9 1525
星月不相逢
星月不相逢 2020-11-28 01:07

I must be missing something trivial with SQLAlchemy\'s cascade options because I cannot get a simple cascade delete to operate correctly -- if a parent element is a deleted,

相关标签:
9条回答
  • 2020-11-28 01:34

    Steven's answer is solid. I'd like to point out an additional implication.

    By using relationship, you're making the app layer (Flask) responsible for referential integrity. That means other processes that access the database not through Flask, like a database utility or a person connecting to the database directly, will not experience those constraints and could change your data in a way that breaks the logical data model you worked so hard to design.

    Whenever possible, use the ForeignKey approach described by d512 and Alex. The DB engine is very good at truly enforcing constraints (in an unavoidable way), so this is by far the best strategy for maintaining data integrity. The only time you need to rely on an app to handle data integrity is when the database can't handle them, e.g. versions of SQLite that don't support foreign keys.

    If you need to create further linkage among entities to enable app behaviors like navigating parent-child object relationships, use backref in conjunction with ForeignKey.

    0 讨论(0)
  • 2020-11-28 01:37

    @Steven's asnwer is good when you are deleting through session.delete() which never happens in my case. I noticed that most of the time I delete through session.query().filter().delete() (which doesn't put elements in the memory and deletes directly from db). Using this method sqlalchemy's cascade='all, delete' doesn't work. There is a solution though: ON DELETE CASCADE through db (note: not all databases support it).

    class Child(Base):
        __tablename__ = "children"
    
        id = Column(Integer, primary_key=True)
        parent_id = Column(Integer, ForeignKey("parents.id", ondelete='CASCADE'))
    
    class Parent(Base):
        __tablename__ = "parents"
    
        id = Column(Integer, primary_key=True)
        child = relationship(Child, backref="parent", passive_deletes=True)
    
    0 讨论(0)
  • 2020-11-28 01:44

    TLDR: If the above solutions don't work, try adding nullable=False to your column.

    I'd like to add a small point here for some people who may not get the cascade function to work with the existing solutions (which are great). The main difference between my work and the example was that I used automap. I do not know exactly how that might interfere with the setup of cascades, but I want to note that I used it. I am also working with a SQLite database.

    I tried every solution described here, but rows in my child table continued to have their foreign key set to null when the parent row was deleted. I'd tried all the solutions here to no avail. However, the cascade worked once I set the child column with the foreign key to nullable = False.

    On the child table, I added:

    Column('parent_id', Integer(), ForeignKey('parent.id', ondelete="CASCADE"), nullable=False)
    Child.parent = relationship("parent", backref=backref("children", passive_deletes=True)
    

    With this setup, the cascade functioned as expected.

    0 讨论(0)
提交回复
热议问题