From 7384522a84e0f1d80ff471e4023f865c84768b2c Mon Sep 17 00:00:00 2001 From: erdgeist Date: Mon, 6 Jul 2026 04:36:36 +0200 Subject: Add FK with ON DELETE CASCADE on occurrences.event_id Was the last remaining gap in the occurrence-orphaning fix from earlier this session: dependent: :destroy on Event#occurrences covers deletion through ActiveRecord, this covers anything that bypasses it (raw SQL, Model.delete). Confirmed zero existing orphans and zero NULL event_ids before adding the constraint (event_id stays nullable - an occurrence without an event is valid, one with a bogus event_id is not). Also adds the index that should have existed on this column already - Postgres needs one for the cascade to be anything other than a full table scan per delete, and none existed before this. deferrable: :immediate added so legitimate application code can still defer the check within a transaction if ever needed - unrelated to, and not a fix for, the fixture-loading issue below. Applying this constraint broke fixture loading entirely (config fix: 81a07bf) and exposed a live bug in events#index (fix: 970f108), both already committed separately - this migration file itself just never got staged until now. --- .../20260706014551_add_foreign_key_on_occurrences_event_id.rb | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 db/migrate/20260706014551_add_foreign_key_on_occurrences_event_id.rb diff --git a/db/migrate/20260706014551_add_foreign_key_on_occurrences_event_id.rb b/db/migrate/20260706014551_add_foreign_key_on_occurrences_event_id.rb new file mode 100644 index 0000000..a8df681 --- /dev/null +++ b/db/migrate/20260706014551_add_foreign_key_on_occurrences_event_id.rb @@ -0,0 +1,6 @@ +class AddForeignKeyOnOccurrencesEventId < ActiveRecord::Migration[8.1] + def change + add_index :occurrences, :event_id + add_foreign_key :occurrences, :events, on_delete: :cascade, deferrable: :immediate + end +end -- cgit v1.3