File tree Expand file tree Collapse file tree 1 file changed +4
-10
lines changed
Expand file tree Collapse file tree 1 file changed +4
-10
lines changed Original file line number Diff line number Diff line change @@ -16,18 +16,12 @@ ALTER TABLE public.order_items
1616 ADD COLUMN IF NOT EXISTS product_name text ,
1717 ADD COLUMN IF NOT EXISTS variant_name text ;
1818
19- -- 2. Back-fill existing rows from the joined product/variant tables so historical
20- -- orders aren't left with NULLs where the product still exists.
21- UPDATE public .order_items oi
19+ UPDATE public .order_items
2220SET
23- product_name = COALESCE(oi .product_name , p .title ),
24- variant_name = COALESCE(oi .variant_name , pv .name )
25- FROM
26- public .products p
27- LEFT JOIN public .product_variants pv ON pv .id = oi .variant_id
21+ product_name = COALESCE(product_name, (SELECT title FROM public .products p WHERE p .id = product_id)),
22+ variant_name = COALESCE(variant_name, (SELECT name FROM public .product_variants pv WHERE pv .id = variant_id))
2823WHERE
29- p .id = oi .product_id
30- AND (oi .product_name IS NULL OR oi .variant_name IS NULL );
24+ product_name IS NULL OR (variant_name IS NULL AND variant_id IS NOT NULL );
3125
3226-- 3. Add index on order_id if missing (speeds up order detail page queries)
3327CREATE INDEX IF NOT EXISTS idx_order_items_order_id
You can’t perform that action at this time.
0 commit comments