account_id
您好,我们有一个名为 billing_infos 的表,其中在三个字段、state_id
和上创建了部分索引,deleted_at
其中deleted_at
是NULL
,我的期望是插入操作在尝试为account_id
、state_id
、插入重复值时应该失败deleted_at = null
。但它似乎正在创建另一个具有重复项的条目。当我们检查空条件时,部分索引是否有任何警告,我已经浏览了官方文档但找不到,下面是我的表架构的片段
porterbizz=> \d+ application.billing_infos; 桌子
"application.billing_infos"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
-----------------+-----------------------------+-----------+----------+-------------------------------------------------------+----------+--------------+-------------
id | integer | | not null | nextval('application.billing_infos_id_seq'::regclass) | plain | |
account_id | integer | | not null | | plain | |
gst_in | character varying(256) | | not null | | extended | |
gst_reg_address | character varying(256) | | not null | | extended | |
invoice_address | character varying(256) | | not null | | extended | |
state_id | integer | | not null | | plain | |
default | boolean | | not null | false | plain | |
deleted_at | timestamp without time zone | | | | plain | |
Indexes:
"billing_infos_pkey" PRIMARY KEY, btree (id)
"account_id_state_id_deleted_at_uniq_index" UNIQUE, btree (account_id, state_id, deleted_at) WHERE deleted_at IS NULL
请从您的部分唯一索引中删除
deleted_at
,因为null
永远不等于任何东西——甚至不等于另一个null
。如果没有
deleted_at
,则会强制执行唯一性:但包含
deleted_at
在索引中,重复插入会成功:工作小提琴