表结构:
create table example (a_id integer, b_id integer, c_id integer, flag integer);
部分索引:
create unique index u_idx on example (a_id, b_id, c_id) where flag = 0;
我的更新插入代码:
with a_ins_upd as (
Insert into example (a_id, b_id, c_id, flag)
select x.a_id, x.b_id, x.c_id, x.flag
from <input_tableType> x
on conflict (a_id, b_id, c_id) where flag = 0
do update
set
a_id = excluded.a_id,
b_id = excluded.b_id,
c_id = excluded.c_id,
flag = excluded.flag
returning *
)
insert into <some_other_table>
Result Entries, as of now :
Entry_1 是表中已有的数据。 Entry_2 是新输入。新输入应该更新 Entry_1,而不是插入新条目。
我哪里出错了。我希望更新第二个条目,而不是插入。
我试图包括flag
作为索引的一部分。然后,记录也被插入。部分索引不适用于on conflict()
clause?
我如何实现这一点 - 我想忽略记录flag
= 1,存在于表中(就像盲目flag
= 1),并对其他记录(具有的记录)进行合并操作flag
= 0,唯一约束仍然有效(a_id, b_id, c_id)
)。我想过做部分约束/索引并做on conflict
。现在看来很棘手。