如何在Postgres的ON CONFLICT DO UPDATE中的where子句中使用唯一索引?

时间:2018-06-22 23:38:22

标签: postgresql sql-update sql-insert

我有一个带有唯一索引的表,该索引具有where子句,并且我正在尝试使用ON CONFLICT DO UPDATE功能,但是我似乎无法使它以该索引为目标。请参见下面的代码,以获得可复制的示例:

postgres=# create table foo(name text, something_unique text, deleted_at timestamp);
CREATE TABLE
postgres=# insert into foo(name, something_unique) values ('hello', 'world');
INSERT 0 1
postgres=# create unique index on foo(something_unique) where deleted_at is null;
CREATE INDEX
postgres=# insert into foo (name, something_unique) values ('hi', 'world') ON CONFLICT(something_unique) DO UPDATE SET name = excluded.name;
ERROR:  there is no unique or exclusion constraint matching the ON CONFLICT specification
postgres=# \d foo
                     Table "public.foo"
      Column      |            Type             | Modifiers
------------------+-----------------------------+-----------
 name             | text                        |
 something_unique | text                        |
 deleted_at       | timestamp without time zone |
Indexes:
    "foo_something_unique_idx" UNIQUE, btree (something_unique) WHERE deleted_at IS NULL

postgres=# insert into foo (name, something_unique) values ('hi', 'world') ON CONFLICT ON CONSTRAINT foo_something_unique_idx DO UPDATE SET name = excluded.name;
ERROR:  constraint "foo_something_unique_idx" for table "foo" does not exist

有人知道如何定位此唯一索引吗?

0 个答案:

没有答案