summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-06 04:36:36 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-06 04:36:36 +0200
commit7384522a84e0f1d80ff471e4023f865c84768b2c (patch)
tree95013360a3086e6f410e3f2fd8645721826d525f
parent394a2b890686cadd7a3ecb0038ce5d0b744431f4 (diff)
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.
-rw-r--r--db/migrate/20260706014551_add_foreign_key_on_occurrences_event_id.rb6
1 files changed, 6 insertions, 0 deletions
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 @@
1class AddForeignKeyOnOccurrencesEventId < ActiveRecord::Migration[8.1]
2 def change
3 add_index :occurrences, :event_id
4 add_foreign_key :occurrences, :events, on_delete: :cascade, deferrable: :immediate
5 end
6end