-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFIX_FOREIGN_KEY.sql
More file actions
29 lines (24 loc) · 826 Bytes
/
FIX_FOREIGN_KEY.sql
File metadata and controls
29 lines (24 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- 1. Drop existing constraint on rentals table
ALTER TABLE rentals
DROP CONSTRAINT IF EXISTS rentals_item_id_fkey;
-- 2. Re-add constraint with ON DELETE CASCADE
ALTER TABLE rentals
ADD CONSTRAINT rentals_item_id_fkey
FOREIGN KEY (item_id)
REFERENCES items(id)
ON DELETE CASCADE;
-- 3. Also do the same for Notifications if they link to items
ALTER TABLE notifications
DROP CONSTRAINT IF EXISTS notifications_related_item_id_fkey;
ALTER TABLE notifications
ADD CONSTRAINT notifications_related_item_id_fkey
FOREIGN KEY (related_item_id)
REFERENCES items(id)
ON DELETE CASCADE;
ALTER TABLE notifications
DROP CONSTRAINT IF EXISTS notifications_related_rental_id_fkey;
ALTER TABLE notifications
ADD CONSTRAINT notifications_related_rental_id_fkey
FOREIGN KEY (related_rental_id)
REFERENCES rentals(id)
ON DELETE CASCADE;