Created
November 17, 2023 21:52
-
-
Save bq1990/f0b7dffe4345f7def25a2df6cffa89cb to your computer and use it in GitHub Desktop.
Cascade delete SqlAlchemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parent(Model): | |
__tablename__ = "parent" | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String) | |
class Child(Model): | |
__tablename__ = "child" | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String) | |
parent_id = db.Column( | |
db.Integer, | |
db.ForeignKey("parent.id"), | |
nullable=False, | |
) | |
parent = db.relationship( | |
"Parent", | |
foreign_keys=parent_id, | |
backref=backref("childrent", cascade="all, delete"), | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment