Add alembic migration for ondelete=CASCADE on organization child FKs (fixes #96) #103
No reviewers
Labels
No labels
agentic-osint
audit
blocked:commit-ready-failed
blocked:gate-fetch
blocked:gate-fetch
blocked:gate-lint
blocked:gate-plan
blocked:gate-research
blocked:needs-decision
blocked:needs-human
blocked:no-changes
blocked:preflight
blocked:verify-failed
bug
chore
deferred
enhancement
feature-request
in-progress
lint-baseline
lint-cleanup
needs-decompose
needs-triage
performance
quickfix
security/medium
triage-approved
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Mark/osint-hub!103
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/96-org-fk-cascade"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Closes #96.
What
d0723c9addedondelete="CASCADE"to theorg_idforeign keys onOrganizationEmail,OrganizationPhone, andOrganizationDomaininapp/models.py, but no alembic migration was generated — existingdatabases keep the old FKs (NO ACTION), so deleting an organization in
production raises a foreign-key violation on child rows instead of
cascading.
This PR adds one migration (
c9e3721d770e) that drops and recreates thethree FKs with
ON DELETE CASCADE:organization_email_org_id_fkeyorganization_phone_org_id_fkeyorganization_domain_org_id_fkeyConstraint names were taken from the inspection task (SQLModel create_all
emits these FKs unnamed, so Postgres auto-names them
<table>_<column>_fkey) and re-verified on scratch Postgres 16 beforeupgrade.
app/models.pywas not touched; no other migration was modified.Sanity checks (scratch Postgres 16, mirrors create_all schema path)
1.
alembic upgrade headagainst a fresh DB — applies cleanly2.
alembic upgrade headagainst a DB pre-seeded with orgs + child rows — applies cleanlySame single migration applied; org 9001 seeded with 2 emails, 1 phone, 1 domain.
3. Manual DELETE verification (psql output)
Before delete:
DELETE FROM organization WHERE id=9001;:After delete — child rows removed, no FK violation:
4.
alembic downgrade -1— restores previous FKs without ondeleteNote (pre-existing, not introduced here)
alembic upgrade headon a truly empty database fails at the rootmigration
8b74cf400a42(relation "case" does not exist) because thebase tables (
case,user,organization, …) are owned bySQLModel.metadata.create_all(app boot), not by any migration. Verifiedidentical failure with and without this PR's migration. Fresh deployments
bootstrap via
create_all(entrypoint.sh) and then migrations apply ontop; that is the path this migration was verified against.
Closing as superseded: the canonical fix for #96 has landed on
mainvia watcher recovery commit8a891e7(migrationb2c3d4e5f6a8_add_ondelete_cascade_org_child_fks.py, 173 lines). That migration mirrors the provenf101c4scd0a1_add_ondelete_cascade_case_membership_fkpattern from #101 with an_fk_has_cascadeguard for safe re-run / fresh-schema no-op. PR #103 and PR #108 are now both superseded.Thanks for the earlier migration drafts — the discussion on idempotency and the data-preservation pattern informed the landed version.
Pull request closed