🖥️

Real incidents need a real screen.

Open senioreng.dev on your laptop for the full experience.

Pull Request #3104

Add CASCADE DELETE to order_items — clean up when orders are deleted

Automates order_item cleanup by adding an ON DELETE CASCADE constraint so that deleting an order automatically removes its associated line items.

Ready to merge
db/migration/V18_add_cascade_delete_order_items.sql+9 additions-3 deletions
Viewed
...
@@ existing table structure (context) @@
1
-- order_items table (existing, shown for context)
2
-- id BIGINT PRIMARY KEY
3
-- order_id BIGINT REFERENCES orders(id) -- FK to orders
4
-- product_id BIGINT REFERENCES products(id) -- FK to products
5
-- quantity INT
6
-- price NUMERIC(10,2)
7
8
-- Add CASCADE DELETE to order_items for automatic cleanup
...
@@ -0,0 +9,18 @@
9
- ALTER TABLE order_items
10
  DROP CONSTRAINT order_items_product_id_fkey;
11
  -- (no cascade on product FK previously)
9
+ ALTER TABLE order_items
10
+   DROP CONSTRAINT order_items_product_id_fkey,
11
+   ADD CONSTRAINT order_items_product_id_fkey
12
+     FOREIGN KEY (product_id)
13
+     REFERENCES products(id)
14
+     ON DELETE CASCADE;
15
16
-- order_id FK unchanged
PE

priya_eng

Approved review

Looks good, cascade is there. This will automate the cleanup nicely.