From a02ae3e6624e10d31ffb673469462e632b76a0a2 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Fri, 15 May 2026 16:29:57 +0200 Subject: [PATCH] =?UTF-8?q?fix(db):=20in=5Freply=5Fto=5Fid=20FK=20ON=20DEL?= =?UTF-8?q?ETE=20SET=20NULL=20=E2=80=94=20deleting=20a=20thought=20no=20lo?= =?UTF-8?q?nger=20blocks=20if=20it=20has=20replies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/010_fix_reply_fk_on_delete.sql | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 crates/adapters/postgres/migrations/010_fix_reply_fk_on_delete.sql diff --git a/crates/adapters/postgres/migrations/010_fix_reply_fk_on_delete.sql b/crates/adapters/postgres/migrations/010_fix_reply_fk_on_delete.sql new file mode 100644 index 0000000..6bf7922 --- /dev/null +++ b/crates/adapters/postgres/migrations/010_fix_reply_fk_on_delete.sql @@ -0,0 +1,11 @@ +-- Change in_reply_to_id FK from RESTRICT (default) to SET NULL. +-- Previously, deleting a thought that had replies raised a FK violation. +-- With SET NULL, deleting a thought orphans its replies (they survive but +-- lose their parent reference), which is the correct semantic for a +-- threaded social app. +ALTER TABLE thoughts + DROP CONSTRAINT IF EXISTS thoughts_in_reply_to_id_fkey; + +ALTER TABLE thoughts + ADD CONSTRAINT thoughts_in_reply_to_id_fkey + FOREIGN KEY (in_reply_to_id) REFERENCES thoughts(id) ON DELETE SET NULL;