fix: V9迁移使用DO块处理外键约束
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Agent
2026-04-01 00:35:59 +00:00
parent 6446f75239
commit 2c98a799bb

View File

@@ -1,5 +1,4 @@
-- 修改商品表外键约束,级联删除(库存、库存流水)
-- 但订单明细不级联删除,因为已冗余保存商品信息
ALTER TABLE stock DROP CONSTRAINT IF EXISTS stock_product_id_fkey;
ALTER TABLE stock ADD CONSTRAINT stock_product_id_fkey
FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE;
@@ -8,7 +7,13 @@ ALTER TABLE stock_flow DROP CONSTRAINT IF EXISTS stock_flow_product_id_fkey;
ALTER TABLE stock_flow ADD CONSTRAINT stock_flow_product_id_fkey
FOREIGN KEY (product_id) REFERENCES products(product_id) ON DELETE CASCADE;
-- 订单明细不级联删除(已冗余保存商品信息)
-- 订单明细不级联删除(已冗余保存商品信息)
-- 如果约束已存在则修改,不存在则忽略
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM information_schema.table_constraints WHERE constraint_name = 'order_items_product_id_fkey') THEN
ALTER TABLE order_items DROP CONSTRAINT IF EXISTS order_items_product_id_fkey;
END IF;
END $$;
ALTER TABLE order_items ADD CONSTRAINT order_items_product_id_fkey
FOREIGN KEY (product_id) REFERENCES products(product_id);