Postgres unique constraint not enforcing uniqueness

前端 未结 2 1222
忘掉有多难
忘掉有多难 2021-01-12 07:41

Here is my constraint:

CREATE UNIQUE INDEX index_subscriptions_on_user_id_and_class_type_id_and_deleted_at
  ON subscriptions
  USING btree
  (user_id, class         


        
2条回答
  •  太阳男子
    2021-01-12 08:05

    Unique indexes in Postgres are based on values being equal, but NULL is never equal to anything, including other NULLs. Therefore any row with a NULL deleted_at value is distinct from any other possible row - so you can insert any number of them.

    One way around this is to create partial indexes, applying different rules to rows with and without NULLs:

     CREATE UNIQUE INDEX ... ON subscriptions
     (user_id, class_type_id) WHERE deleted_at IS NULL;
    
     CREATE UNIQUE INDEX ... ON subscriptions
     (user_id, class_type_id, deleted_at) WHERE deleted_at IS NOT NULL;
    

提交回复
热议问题