Skip to content

Instantly share code, notes, and snippets.

@bq1990
Created November 17, 2023 21:52
Show Gist options
  • Save bq1990/f0b7dffe4345f7def25a2df6cffa89cb to your computer and use it in GitHub Desktop.
Save bq1990/f0b7dffe4345f7def25a2df6cffa89cb to your computer and use it in GitHub Desktop.
Cascade delete SqlAlchemy
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