fix(structures): align Structure.delegation_code FK action with Prisma default #229
Reference in New Issue
Block a user
Delete Branch "fix/adr-0027-pr1-fk-action-on-delete"
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?
Summary
Single-line fix on the
add_org_hierarchymigration shipped in #228 (ADR-0027 PR 1). The FK action onStructure.delegation_codewasON DELETE RESTRICT(Prisma's default for required relations); the field is optional (Structure.delegation Delegation?), so Prisma's default isON DELETE SET NULL. Mismatch causedprisma migrate devto detect drift and prompt for a corrective migration on every run.Caught locally on first VM-side validation (the workspace dev DB). No production deployment of #228 yet, so the fix is edit-in-place rather than a corrective sibling migration — keeps the repo's migration history clean.
What lands
apps/portal-bff/prisma/migrations/20260526143000_add_org_hierarchy/migration.sqlON DELETE RESTRICT→ON DELETE SET NULLon thestructures_delegation_code_fkeyFK. Inline comment explains the rule (nullable Prisma relation → SET NULL is the matching default; not matching it generates drift on everymigrate dev).That's it. One line of SQL, one comment block. No schema.prisma change, no test change, no other file touched.
Why edit-in-place vs new corrective migration
Edit-in-place is safe here because:
./infra/local/dev.sh down -v) and reapplies — clean migration history, no "modified after applied" warning.prisma/migrations/.Once a non-dev environment has applied a migration, the rule reverses: corrective migration mandatory, never edit in place. ADR-0015's "trunk-based + squash-merge" and the absence of a deployed environment at this stage gives us this one-time window.
Recovery procedure for anyone who applied #228
If anyone has a stray
drift_inspection/folder underprisma/migrations/(artifact of the--create-onlydebugging step), delete it before reapplying — it's not in the repo, it was just diagnostic output.Test plan
node scripts/check-catalogue-drift.mjs— clean (unchanged by this fix).node --test scripts/check-catalogue-drift.spec.mjs— 22 tests passing (unchanged).pnpm exec prettier --checkclean (SQL file unaffected by prettier but verified).prisma migrate devexits clean, no drift prompt.Notes for the reviewer
CLAUDE.mdwith a "remember to match Prisma default actions" rule. If we hit the same mistake on ADR-0026 PR 1'sPerson/User/UserScopemigration, we'll consider promoting it. For now: one inline comment at the place where the rule is non-obvious.Structure.kindin this same migration) are not the same surface as FK referential actions. Catching this required actually runningprisma migrate dev. A possible future improvement: a CI gate that runsprisma migrate diff --from-migrations --to-schema-datamodel --script --shadow-database-url …and fails if the diff is non-empty (zero-drift gate). Worth an ADR amendment if it becomes a recurring class of bug.