![]() (note "Child" as a string: this is allowed when using the declarative style, so that you are able to refer to a class that is not yet defined) If you define the relationship on the Parent class instead, it will work: children = relationship("Child", cascade="all,delete", backref="parent") The problem is that sqlalchemy considers Child as the parent, because that is where you defined your relationship (it doesn't care that you called it "Child" of course). Why? How do I make the children cascade delete? Next, it deletes the parent, but the children persist. The script creates a parent, adds 3 children, then commits. ![]() There is a simple, one-to-many relationship between Parent and Child. Print "Before delete, children = ".format(session.query(Parent).count()) ![]() Parent = relationship(Parent, cascade = "all,delete", backref = "children")Įngine = create_engine("sqlite:///:memory:") Parentid = Column(Integer, ForeignKey(Parent.id)) ![]() I've put a concise test case here: from sqlalchemy import Column, Integer, ForeignKeyįrom import declarative_base 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, the children persist, with null foreign keys. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |