简体   繁体   English

postgres 插入冲突错误 - 没有唯一或排除约束

[英]postgres insert on conflict error - there is no unique or exclusion constraint

I have two tables in postgres daily_report and summary_songs .我在 postgres daily_reportsummary_songs中有两个表。

Sql file for creating the tables is here: https://nofile.io/f/Ef94rMFRh6C/file.sql用于创建表的 SQL 文件位于: https ://nofile.io/f/Ef94rMFRh6C/file.sql

I want to update the summary_songs at the end of each day with below conditions:我想在每天结束时使用以下条件更新summary_songs

  • If the userid already does not exist, then record from daily_report need to be inserted into summary_songs如果userid已经不存在,则需要将daily_report中的记录插入到summary_songs
  • If the userid exists, then summary_songs.countid = summary_songs.countid+ daily_report.countid .如果userid存在,则summary_songs.countid = summary_songs.countid+ daily_report.countid

I used below query to update summary_songs :我使用以下查询来更新summary_songs

insert into summary_songs 
(select * from daily_report as B)
on conflict (userid, songd) 
do update set countid = countid+excluded.countid ;  

I get below error:我收到以下错误:

ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification错误:没有匹配 ON CONFLICT 规范的唯一或排除约束

To use on conflict you need to enforce a unique(userid, songd) on your summary_songs table:on conflict使用,您需要在summary_songs表上强制执行unique(userid, songd)

SQL Fiddle SQL小提琴

PostgreSQL 9.6 Schema Setup : PostgreSQL 9.6 架构设置

CREATE TABLE summary_songs (
    date_time date  NOT NULL,
    userid integer NOT NULL,
    songd integer NOT NULL,
    countid integer NOT NULL,
    unique(userid, songd) 
);


CREATE TABLE daily_report(
    date_time date NOT NULL,
    userid integer NOT NULL,
    songd integer NOT NULL,
    countid integer NOT NULL
);


insert into daily_report (date_time, userid, songd, countid) values
(to_date('2017-12-31','YYYY-MM-DD'),  1 ,     1 ,       5),
(to_date('2017-12-31','YYYY-MM-DD'),  2 ,     1 ,      10),
(to_date('2017-12-31','YYYY-MM-DD'),  4 ,     1 ,       7);


insert into summary_songs (date_time, userid, songd, countid) values
(to_date('2017-12-30', 'YYYY-MM-DD'),1, 1,  80),
(to_date('2017-12-30', 'YYYY-MM-DD'),2, 1,  51),
(to_date('2017-12-30', 'YYYY-MM-DD'),3, 1,  66);

Query 1 :查询 1

select * from daily_report 

Results :结果

|  date_time | userid | songd | countid |
|------------|--------|-------|---------|
| 2017-12-31 |      1 |     1 |       5 |
| 2017-12-31 |      2 |     1 |      10 |
| 2017-12-31 |      4 |     1 |       7 |

Query 2 :查询 2

select * from summary_songs 

Results :结果

|  date_time | userid | songd | countid |
|------------|--------|-------|---------|
| 2017-12-30 |      1 |     1 |      80 |
| 2017-12-30 |      2 |     1 |      51 |
| 2017-12-30 |      3 |     1 |      66 |

Query 3 :查询 3

insert into summary_songs (date_time, userid, songd, countid)
select date_time, userid, songd, countid from daily_report
on conflict (userid, songd) 
do update set 
  countid = summary_songs.countid + excluded.countid ,
  date_time = excluded.date_time

Query 4 :查询 4

select * from summary_songs 

Results :结果

|  date_time | userid | songd | countid |
|------------|--------|-------|---------|
| 2017-12-30 |      3 |     1 |      66 |
| 2017-12-31 |      1 |     1 |      85 |
| 2017-12-31 |      2 |     1 |      61 |
| 2017-12-31 |      4 |     1 |       7 |

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

相关问题 错误:没有与 ON CONFLICT 规范匹配的唯一或排除约束 - ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification 没有与 ON CONFLICT 匹配的唯一或排除约束 - No unique or exclusion constraint matching the ON CONFLICT 错误:pq:没有与 ON CONFLICT 规范匹配的唯一或排除约束 - ERROR: pq: there is no unique or exclusion constraint matching the ON CONFLICT specification 'unique' 约束的奇怪情况(可能的排除情况)| Postgres - wierd case of 'unique' constraint (possible case for exclusion) | Postgres Postgers:使用主键和 UNIQUE 列时,没有唯一或排除约束匹配 ON CONFLICT 规范 - Postgers: No unique or exclusion constraint matching ON CONFLICT specification when using a primary key and a UNIQUE column postgres约束排除:无性能提升 - postgres constraint exclusion: no performance gain Postgres ON CONFLICT ON CONSTRAINT 在错误日志中触发错误 - Postgres ON CONFLICT ON CONSTRAINT triggering errors in the error log 插入并检测唯一约束错误 - INSERT and detect unique constraint error 在冲突中使用插入时 Postgres 语法错误 - Postgres syntax error while using insert on conflict 如何在不违反唯一约束的情况下将1行插入POSTGRES_DB - How to INSERT 1 row into POSTGRES_DB without unique constraint violation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM