Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

python sqlalchemy. Set up ONDELETE CASCADE for both tables: parent and child

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...