Closed jymchng closed 1 year ago
Hello @jymchng did you tried cascade: "all, delete-orphan"
as here
Or try this
class Project(BaseIntModel, ProjectBase, table=True):
categories: List[Category] = Relationship(
back_populates='project',
sa_relationship_kwargs={
"lazy": "selectin",
'cascade': 'all,delete',
"passive_deletes": True, # Add this line to enable passive deletes
"primaryjoin": "Category.project_id==Project.id",
})
class Category(BaseUUIDModel, CategoryBase, table=True):
project: Optional["Project"] = Relationship(
back_populates="categories",
sa_relationship_kwargs={
"lazy": "selectin",
"primaryjoin": "Category.project_id==Project.id",
"passive_deletes": True, # Add this line to enable passive deletes
})
Hi @jonra1993
I have two tables,
Project
andCategory
. One row inProject
can be linked to many rows inCategory
, i.e. one project can belong to many categories.Project.categories: List[Category]
andCategory.project: Project
.Right now, this is my
Category
table:And this is my
Project
table:What
sa_relationship_kwargs
I need to change in order to have this effect that whenever I delete a record inProject
, all rows inCategory
such thatCategory.project_id == Project.id
are deleted?