295eb22826
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
24 lines
1.1 KiB
SQL
24 lines
1.1 KiB
SQL
-- 017_pair_tokens_fk_cascade.sql — fix the self-contradictory FK on
|
|
-- pair_tokens.created_by.
|
|
--
|
|
-- Migration 006 declared:
|
|
-- created_by BIGINT NOT NULL REFERENCES users(id) ON DELETE SET NULL
|
|
-- which is impossible: ON DELETE SET NULL tries to write NULL into a NOT NULL
|
|
-- column, so deleting an admin that had ever minted a pair token would fail
|
|
-- with a not-null-violation instead of cleaning up the tokens.
|
|
--
|
|
-- Forward-safe fix: drop the old FK constraint and re-add it as ON DELETE
|
|
-- CASCADE. Deleting a user now removes the (short-lived, mostly-expired) pair
|
|
-- tokens they created, which is the sane behavior for onboarding tokens.
|
|
--
|
|
-- The constraint name from an inline column REFERENCES is auto-generated by
|
|
-- Postgres as <table>_<column>_fkey. We drop it defensively with IF EXISTS so
|
|
-- this migration is safe even if the name ever differed.
|
|
|
|
ALTER TABLE pair_tokens
|
|
DROP CONSTRAINT IF EXISTS pair_tokens_created_by_fkey;
|
|
|
|
ALTER TABLE pair_tokens
|
|
ADD CONSTRAINT pair_tokens_created_by_fkey
|
|
FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE;
|