在 postgres 中的一行中添加部分唯一性约束,以便可以复制一个值

Add partial uniqueness constraint in postgres on a row such that one value is okay to duplicate

我想创建一个 table 的电子邮件 emails,其中“电子邮件”列中的所有值都必须是唯一的,但“redacted@redacted.com”占位符除外。示例:

我在 postgres 文档中看到允许 null 值不重复很简单,但是有没有办法用另一个值做到这一点?

您可以创建过滤的唯一索引:

create unique index unq_emails_email on (email)
    where email <> 'redacted@redacted.com';