简体   繁体   English

如何在排除约束条件下使用ON CONFLICT?

[英]How to use ON CONFLICT with Exclusion Constraint?

for example: index by userid , sdate , edate 例如:index by useridsdateedate

userid  sdate       edate
001     2019-01-01  2019-01-30

if I insert new data like: 如果我插入新数据,如:

userid  sdate       edate           
001     2019-01-03  2019-01-20  
   or
001     2019-01-13  2019-02-10  
   or
001     2019-02-01  2019-02-15  

I tried below using GIST, but how to combine it using ON CONFLICT ? 我在下面尝试使用GIST,但如何使用ON CONFLICT组合它?

CREATE EXTENSION btree_gist

CREATE TABLE test(
   ID INT PRIMARY KEY     NOT NULL,
   USERID     CHAR(5),
   SDATE      DATE,
   EDATE      DATE,
   EXCLUDE USING gist
   (USERID WITH =,
   daterange(SDATE, EDATE, '[]') WITH &&)
);


Insert Into test (usersid, sdate, edate)
Values  (@UsersId, @SDate, @EDate) 
ON Conflict ON CONSTRAINT  test_userid_daterange_excl 
Do Update 
   Set sdate = @SDate, edate = @EDate

I got: 我有:

ERROR:  ON CONFLICT DO UPDATE not supported with exclusion constraints

based on the above scenario, I expect, the following: 根据上述情况,我希望如下:

userid  sdate       edate       I_EXPECT    
001     2019-01-03  2019-01-20  UPDATE because it is in range
001     2019-01-13  2019-02-10  UPDATE because it is in range
001     2019-02-01  2019-02-15  INSERT because it is not in range

select version() shows: select version()显示:

PostgreSQL 10.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit

You cannot use INSERT ... ON CONFLICT with an exclusion index, like the error message says. 您不能使用INSERT ... ON CONFLICT与排除索引,如错误消息所示。

You will have to use code like in example 43.2 from the documentation . 您必须使用文档中的示例43.2中的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有与 ON CONFLICT 匹配的唯一或排除约束 - No unique or exclusion constraint matching the ON CONFLICT 错误:没有与 ON CONFLICT 规范匹配的唯一或排除约束 - ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification postgres 插入冲突错误 - 没有唯一或排除约束 - postgres insert on conflict error - there is no unique or exclusion constraint 如何施加这种排除约束? - How to impose this exclusion constraint? 如何在表中未定义约束的情况下修复“没有与 ON CONFLICT 规范匹配的唯一或排除约束” - How to fix the “ there is no unique or exclusion constraint matching the ON CONFLICT specification ” without defined contraint in table 当我指定它时,如何修复“这里没有与 ON CONFLICT 规范匹配的唯一或排除约束”? - How can I fix t"here is no unique or exclusion constraint matching the ON CONFLICT specification" when I have specified it? 冲突部分索引的 Postgres 唯一或排除约束无法更新票证 - Postgres unique or exclusion constraint for partial index on conflict fails to update tickets POSTGRESQL:错误:没有与 ON CONFLICT 规范匹配的唯一或排除约束 - POSTGRESQL: ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification 错误:pq:没有与 ON CONFLICT 规范匹配的唯一或排除约束 - ERROR: pq: there is no unique or exclusion constraint matching the ON CONFLICT specification 是否可以在排除约束中使用IS NOT DISTINCT FROM? - Is it possible to use IS NOT DISTINCT FROM in an exclusion constraint?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM