fix: delete answers before questions in update_test to avoid FK violation
Bulk DELETE bypasses ORM cascade — must manually delete child rows first. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -170,6 +170,11 @@ async def update_test(test_id: int, data: TestCreate, db: AsyncSession = Depends
|
||||
test.time_limit = data.time_limit
|
||||
test.allow_navigation_back = data.allow_navigation_back
|
||||
|
||||
# Сначала удаляем ответы (FK: answers.question_id → questions.id)
|
||||
q_ids_result = await db.execute(select(Question.id).where(Question.test_id == test_id))
|
||||
q_ids = [row[0] for row in q_ids_result.fetchall()]
|
||||
if q_ids:
|
||||
await db.execute(delete(Answer).where(Answer.question_id.in_(q_ids)))
|
||||
await db.execute(delete(Question).where(Question.test_id == test_id))
|
||||
await db.flush()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user