bmc_hub/migrations/999_fix_cvr_unique_constraint.sql

14 lines
575 B
MySQL
Raw Normal View History

-- Migration: Fix CVR Unique Constraint
-- Allow multiple customers without CVR number (NULL or empty string)
-- Drop old unique constraint
ALTER TABLE customers DROP CONSTRAINT IF EXISTS customers_cvr_number_key;
-- Create partial unique index - only enforce uniqueness for non-empty CVR numbers
CREATE UNIQUE INDEX IF NOT EXISTS customers_cvr_number_unique_idx
ON customers (cvr_number)
WHERE cvr_number IS NOT NULL AND cvr_number != '';
COMMENT ON INDEX customers_cvr_number_unique_idx IS
'Ensures CVR numbers are unique, but allows multiple customers without CVR';