Browse Source

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>
master
Aleksey Razorvin 1 week ago
parent
commit
0977eb0c38
  1. 5
      backend/app/api/tests.py

5
backend/app/api/tests.py

@ -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()

Loading…
Cancel
Save