-- 084_multi_code_per_agent.sql -- Allow a single sales agent (person/company) to have MULTIPLE referral codes, -- each with its own client discount and agent commission split. -- -- Background: each sales_agents row already represents one referral code -- (it owns a unique agent_code and links to one discount_code). The only thing -- that blocked the same person from having several codes was the UNIQUE -- constraint on sales_agents.email. We drop that constraint (keeping a plain -- index for lookups) so the same email can back several codes/splits. -- -- All commission/referral lookups key on the unique agent_code, so no other -- logic changes are required at the DB level. BEGIN; -- Drop the UNIQUE constraint on email (name is auto-generated by Postgres). ALTER TABLE sales_agents DROP CONSTRAINT IF EXISTS sales_agents_email_key; -- Keep a non-unique index so email lookups stay fast. -- (idx_agents_email already exists from migration 011; create defensively.) CREATE INDEX IF NOT EXISTS idx_agents_email ON sales_agents (email); COMMIT;