当给定列与 PostgreSQL 中的谓词匹配时,考虑行是唯一的

Consider rows unique when a given column matches predicate in PostgreSQL

在唯一性约束的上下文中,如果给定列具有特定值,例如 "false".

,是否有一种方法可以认为行是唯一的

例如,

这是允许的:

id  | user| location| active
----+-----+----------+----------
  1 |   1 | West LA  | f
  2 |   1 | West LA  | f
  3 |   1 | West LA  | t
  4 |   2 | West LA  | t

但这不会:

id  | user| location| active
----+-----+----------+----------
  1 |   1 | West LA  | f
  2 |   1 | West LA  | t
  3 |   1 | West LA  | t
  4 |   2 | West LA  | t

为了实现这一点,我想知道是否可以做一个类似这样的唯一性约束:
UNIQUE(user, location) 但只应用它 WHERE active = true

您快完成了 - 创建部分(又名 "filtered")唯一索引:

create unique index on the_table (day, location) 
   where active;