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

CREATE TABLE company_relationships (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), purchaser_company_id UUID NOT NULL REFERENCES companies(id), factory_company_id UUID NOT NULL REFERENCES companies(id), factory_type factory_type NOT NULL, status TEXT NOT NULL DEFAULT 'active', created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), UNIQUE(purchaser_company_id, factory_company_id)); CREATE INDEX idx_relationships_purchaser ON company_relationships(purchaser_company_id); CREATE INDEX idx_relationships_factory ON company_relationships(factory_company_id); ALTER TABLE company_relationships ENABLE ROW LEVEL SECURITY; CREATE POLICY company_select_relationships ON company_relationships FOR SELECT USING (purchaser_company_id = get_user_master_company_id() OR factory_company_id = get_user_master_company_id()); CREATE POLICY company_insert_relationships ON company_relationships FOR INSERT WITH CHECK (purchaser_company_id = get_user_master_company_id());
