fix(models): add explicit ondelete rules to all foreign keys#79
Open
tongshu2023 wants to merge 1 commit into
Open
fix(models): add explicit ondelete rules to all foreign keys#79tongshu2023 wants to merge 1 commit into
tongshu2023 wants to merge 1 commit into
Conversation
…56#36) Audit of apps/api/models/ found 22 ForeignKey definitions without an explicit ondelete. All are now explicit, with a matching Alembic migration (20260610_0024) that drops and recreates the constraints on Postgres (SQLite local mode gets the schema from create_all + PRAGMA foreign_keys=ON). CASCADE (owned child rows die with their parent): - courses.user_id; course_content_tree.course_id / parent_id (subtree) - conversation_memories.user_id; generated_assets.user_id - ingestion_jobs.user_id; practice_problems.course_id - practice_results.problem_id / user_id - scrape_sources.user_id / course_id; auth_sessions.user_id - study_plans.user_id / course_id; usage_events.user_id SET NULL (soft references that should survive parent deletion): - generated_assets.course_id (assets persist after course delete) - conversation_memories.course_id; usage_events.course_id (analytics) - assignments.source_ingestion_id; scrape_sources.last_ingestion_id - practice_problems.content_node_id / parent_problem_id Adds tests/test_fk_cascade.py exercising DB-level cascade and SET NULL behavior via Core DELETEs on a real SQLite database with FK enforcement. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #36.
An audit of
apps/api/models/found 22 ForeignKey definitions without an explicitondelete. Deleting a user or course left orphaned rows (memories, practice results, usage events, scrape sources, content trees) in the database indefinitely. All foreign keys now carry an explicit rule, with a matching Alembic migration.Policy
CASCADE — owned child rows die with their parent:
courses.user_idcourse_content_tree.course_id/parent_idcascade="all, delete-orphan")conversation_memories.user_idgenerated_assets.user_idingestion_jobs.user_idpractice_problems.course_idpractice_results.problem_id/user_idscrape_sources.user_id/course_idauth_sessions.user_idstudy_plans.user_id/course_idusage_events.user_idSET NULL — soft references that should be preserved:
conversation_memories.course_idgenerated_assets.course_idassignments.source_ingestion_idpractice_problems.content_node_idpractice_problems.parent_problem_idscrape_sources.last_ingestion_idusage_events.course_idMigration
20260610_0024_fk_ondelete_rules.py(revises2870051cd576, single head verified):fk_<table>_<col>_<reftable>name and the new rule; inspector-guarded per table/column like the house pattern (20260303_0012).env.pyskips Alembic for sqlite URLs and the schema comes fromBase.metadata.create_all()+PRAGMA foreign_keys=ON, so the model-level changes take effect there directly.downgrade()restores the constraints withoutondelete.Note: the Postgres path follows the inspector-driven house pattern but was validated locally against SQLite only (no PG instance here); a staging
alembic upgrade headbefore merge would be a sensible extra check.Tests
tests/test_fk_cascade.py(4 new) on real in-memory SQLite withPRAGMA foreign_keys=ON, using CoreDELETEto bypass ORM-level cascades and exercise the DB rules themselves:generated_assets.course_id,conversation_memories.course_id,usage_events.course_idLocal results:
test_fk_cascade.py + test_loom.py + test_services.py→ 90 passed; full suite (tests/ --ignore=tests/e2e) → 983 passed, 4 failed, 5 skipped — the same 4 failures reproduce on unmodifiedmain(local proxy / missing camoufox / sandbox backend environment issues), zero new failures.Acceptance criteria
ondeleterules🤖 Generated with Claude Code