-- ============================================
-- 此文件由 Meoo Cloud 自动生成，请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================


-- 修改 plan_factories 的 INSERT 策略，允许创建不关联具体计划的工厂记录
DROP POLICY IF EXISTS users_insert_plan_factories ON plan_factories;

CREATE POLICY users_insert_plan_factories ON plan_factories
 FOR INSERT
 WITH CHECK (
 -- 允许创建关联到当前用户计划的记录
 EXISTS (
 SELECT 1 FROM production_plans
 WHERE production_plans.id = plan_factories.plan_id
 AND production_plans.purchaser_id = get_user_master_company_id()
 )
 OR
 -- 允许创建虚拟 plan_id (全零UUID) 的记录，用于工厂管理
 plan_factories.plan_id = '00000000-0000-0000-0000-000000000000'::uuid
 );

