I have 2 tables with relation 1 to 1, I want to remove both rows if any of them (parent or child) was deleted. Important note: deletion should be on the database layer, not the adapter layer because sometimes I'm using the database directly (I'm using postgres).
class InterestMeta(Base):
__tablename__ = "interests_meta"
id = Column(Integer, primary_key=True, index=True)
status = Column(Intege, nullable=False)
interests_data = relationship(
"InterestData", uselist=False,
back_populates="interests_meta",
cascade="all, delete",
passive_deletes=True)
users_meta_id = Column(Integer, ForeignKey('users_meta.id', ondelete='CASCADE'))
users_meta = relationship("UserMeta", back_populates="interests_meta")
class InterestData(Base):
__tablename__ = "interests_data"
id = Column(Integer, primary_key=True, index=True)
content_type = Column(String, nullable=False)
interests_meta_id = Column(Integer, ForeignKey('interests_meta.id',
ondelete='CASCADE'))
question from:
https://stackoverflow.com/questions/65625791/python-sqlalchemy-set-up-ondelete-cascade-for-both-tables-parent-and-child 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…