简体   繁体   English

错误:pq:没有与 ON CONFLICT 规范匹配的唯一或排除约束

[英]ERROR: pq: there is no unique or exclusion constraint matching the ON CONFLICT specification

I have this visitor table structure like below and I have created the upsert procedure to this table.我有一个如下所示的访问者表结构,我已经为这个表创建了 upsert 过程。 When I call the procedure it returns ERROR: pq: there is no unique or exclusion constraint matching the ON CONFLICT specification .当我调用该过程时,它返回ERROR: pq: there is no unique or exclusion constraint matching the ON CONFLICT specification Can anyone please suggest the required changes.任何人都可以建议所需的更改。


CREATE TABLE visitors
(
    id               SERIAL    NOT NULL,

    facebook         TEXT      DEFAULT NULL,
    github           TEXT      DEFAULT NULL,
    linkedin         TEXT      NOT NULL,
    twitter          TEXT      DEFAULT NULL
);

ALTER TABLE visitors ADD CONSTRAINT visitors_primary_key PRIMARY KEY (id);

ALTER TABLE visitors ADD CONSTRAINT visitors_urls UNIQUE (facebook, github, linkedin, twitter);

The upsert procedure:插入过程:

CREATE OR REPLACE PROCEDURE visitors_upsert
(
    _facebook             TEXT      DEFAULT NULL,
    _github               TEXT      DEFAULT NULL,
    _linkedin             TEXT      DEFAULT '',
    _twitter              TEXT      DEFAULT NULL
)
LANGUAGE SQL
AS $$
    INSERT INTO visitors
    (
        facebook,
        github,
        linkedin
    )
    VALUES
    (
        _facebook,
        _github,
        _linkedin,
        _twitter
    )
    ON CONFLICT (linkedin)
    DO UPDATE 
    SET
        facebook = _facebook,
        github = _github,
        twitter = _twitter
$$;
CALL visitors_upsert('facebook', 'github', 'linkedin', 'twitter')

Demo: https://dbfiddle.uk/?rdbms=postgres_13&fiddle=21ed4fa7526a0744feecbbe43f8944f6演示: https : //dbfiddle.uk/?rdbms=postgres_13&fiddle=21ed4fa7526a0744feecbbe43f8944f6

There is no unique constraint on linkedin . linkedin没有唯一约束。 Either create one, or use something else than INSERT ... ON CONFLICT .要么创建一个,要么使用INSERT ... ON CONFLICT以外的其他东西。

暂无
暂无

声明:本站的技术帖子网页,遵循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 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 insert on conflict error - there is no unique or exclusion constraint 错误:具有FK时没有唯一约束匹配 - ERROR: No unique constraint matching when having FK 错误:没有唯一约束匹配给定表的键 - ERROR: No unique constraint matching given keys for referenced table 如何修复错误“没有唯一约束匹配引用表的给定键” - How to fix error "there is no unique constraint matching given keys for referenced table" "错误:没有唯一约束匹配引用表“事件”的给定键" - error: there is no unique constraint matching given keys for referenced table "incident" 无法解释此错误:没有与引用表的给定键匹配的唯一约束 - Unable to explain this error: there is no unique constraint matching given keys for referenced table Postgresql错误:没有唯一约束匹配给定键的引用表 - Postgresql ERROR: there is no unique constraint matching given keys for referenced table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM