I just want to delete a survey record in the Survey
table, and the record in SurveyQuestions
should be deleted too. I've tried cascade
, passive_deletes
, and ondelete
. I keep getting the foreign key violation error no matter what I try from the documentation. Is it the way my tables are set up?
class Survey(Base):
__tablename__ = 'survey'
id = Column(Integer, primary_key=True, autoincrement=True)
survey_description = Column(String(100))
survey_start_date = Column(Date)
survey_end_date = Column(Date)
survey_is_active = Column(Boolean)
survey_questions = relationship(Question, secondary='survey_questions',cascade="all, delete",passive_deletes=True)
class SurveyQuestions(Base):
__tablename__ = 'survey_questions'
id = Column(Integer, primary_key=True, autoincrement=True)
survey_id = Column(Integer, ForeignKey('survey.id', ondelete='CASCADE'))
question_id = Column(Integer, ForeignKey('question.id', ondelete='CASCADE'))
Are there other models that use Survey
as a foreign key? Perhaps the foreign key violation happens because you didn't specify ondelete
strategy in another model that points to Survey
So I had to drop all the tables using Base.metadata.drop_all(engine)
before the foreign key relations would take effect. Since then we have adopted alembic to remedy this issue through the use of migrations.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.