处理因插入重复值而导致的唯一约束异常

时间:2018-03-06 14:03:17

标签: sql postgresql duplicates postgresql-9.6 django-postgresql

我有这个表(DDL):

CREATE TABLE corpname_skill
(
  id                                      SERIAL  NOT NULL
    CONSTRAINT corpname_skill_pkey
    PRIMARY KEY,
  guid                                    UUID,
  user_id                                 VARCHAR(50),
  user_strenght_area_name_1               VARCHAR(300),
  user_strenght_area_name_2               VARCHAR(300),
  user_strenght_area_name_3               VARCHAR(300),
  user_strenght_area_name_4               VARCHAR(300),
  user_development_area_name_1            VARCHAR(300),
  user_development_area_name_2            VARCHAR(300),
  user_development_area_name_3            VARCHAR(300),
  user_development_area_name_4            VARCHAR(300),
  line_manager_strenght_area_name_1       VARCHAR(300),
  line_manager_strenght_area_name_2       VARCHAR(300),
  line_manager_strenght_area_name_3       VARCHAR(300),
  line_manager_strenght_area_name_4       VARCHAR(300),
  line_manager_strenght_area_comment_1    TEXT    NOT NULL,
  line_manager_strenght_area_comment_2    TEXT    NOT NULL,
  line_manager_strenght_area_comment_3    TEXT    NOT NULL,
  line_manager_strenght_area_comment_4    TEXT    NOT NULL,
  line_manager_development_area_name_1    VARCHAR(300),
  line_manager_development_area_name_2    VARCHAR(300),
  line_manager_development_area_name_3    VARCHAR(300),
  line_manager_development_area_name_4    VARCHAR(300),
  line_manager_development_area_comment_1 TEXT,
  line_manager_development_area_comment_2 TEXT,
  line_manager_development_area_comment_3 TEXT,
  line_manager_development_area_comment_4 TEXT,
  line_manager_final_comment              TEXT,
  line_manager_step_status                VARCHAR(300),
  company_profile_id                         INTEGER NOT NULL
    CONSTRAINT corpname_skill_company_profile_id_key
    UNIQUE
    CONSTRAINT corpname_skill_company_profile_id_ae37e790_fk_corpname
    REFERENCES corpname_corpnameuserprofile
      DEFERRABLE INITIALLY DEFERRED,
  task_closing_date                       DATE
);

我正在尝试插入那里:

INSERT INTO corpname_skill (
    guid, 
    user_id, 
    user_strenght_area_name_1, 
    user_strenght_area_name_2, 
    user_strenght_area_name_3, 
    user_strenght_area_name_4, 
    user_development_area_name_1, 
    user_development_area_name_2, 
    user_development_area_name_3, 
    line_manager_strenght_area_name_1, 
    line_manager_strenght_area_name_2, 
    line_manager_strenght_area_name_3, 
    line_manager_strenght_area_name_4, 
    line_manager_strenght_area_comment_1, 
    line_manager_strenght_area_comment_2, 
    line_manager_strenght_area_comment_3, 
    line_manager_strenght_area_comment_4, 
    line_manager_development_area_name_1, 
    line_manager_development_area_name_2, 
    line_manager_development_area_name_3, 
    line_manager_development_area_comment_1, 
    line_manager_development_area_comment_2, 
    line_manager_development_area_comment_3, 
    line_manager_final_comment, 
    line_manager_step_status, 
    task_closing_date, 
    company_profile_id
)
SELECT
    t.guid, 
    t.user_id, 
    t.user_strenght_area_name_1, 
    t.user_strenght_area_name_2, 
    t.user_strenght_area_name_3, 
    t.user_strenght_area_name_4, 
    t.user_development_area_name_1, 
    t.user_development_area_name_2, 
    t.user_development_area_name_3, 
    t.line_manager_strenght_area_name_1, 
    t.line_manager_strenght_area_name_2, 
    t.line_manager_strenght_area_name_3, 
    t.line_manager_strenght_area_name_4, 
    t.line_manager_strenght_area_comment_1, 
    t.line_manager_strenght_area_comment_2, 
    t.line_manager_strenght_area_comment_3, 
    t.line_manager_strenght_area_comment_4, 
    t.line_manager_development_area_name_1, 
    t.line_manager_development_area_name_2, 
    t.line_manager_development_area_name_3, 
    t.line_manager_development_area_comment_1, 
    t.line_manager_development_area_comment_2, 
    t.line_manager_development_area_comment_3, 
    t.line_manager_final_comment, 
    t.line_manager_step_status, 
    t.task_closing_date, 
    t.company_profile_id

FROM corpname_skill o
RIGHT JOIN corpname_skill_new t
ON t.company_profile_id = o.company_profile_id
WHERE o.company_profile_id IS NULL

但我遇到了一个独特的约束错误。

[23505] ERROR: duplicate key value violates unique constraint "corpname_skill_company_profile_id_key" Detail: Key (company_profile_id)=(256871) already exists.

我使用postgres docs中描述的BEGIN...EXCEPTION...END;块包装了sql语句:

BEGIN
    INSERT INTO corpname_skill (
        guid, 
        user_id, 
        user_strenght_area_name_1, 
        user_strenght_area_name_2, 
        user_strenght_area_name_3, 
        user_strenght_area_name_4, 
        user_development_area_name_1, 
        user_development_area_name_2, 
        user_development_area_name_3, 
        line_manager_strenght_area_name_1, 
        line_manager_strenght_area_name_2, 
        line_manager_strenght_area_name_3, 
        line_manager_strenght_area_name_4, 
        line_manager_strenght_area_comment_1, 
        line_manager_strenght_area_comment_2, 
        line_manager_strenght_area_comment_3, 
        line_manager_strenght_area_comment_4, 
        line_manager_development_area_name_1, 
        line_manager_development_area_name_2, 
        line_manager_development_area_name_3, 
        line_manager_development_area_comment_1, 
        line_manager_development_area_comment_2, 
        line_manager_development_area_comment_3, 
        line_manager_final_comment, 
        line_manager_step_status, 
        task_closing_date, 
        company_profile_id
    )
    SELECT
        t.guid, 
        t.user_id, 
        t.user_strenght_area_name_1, 
        t.user_strenght_area_name_2, 
        t.user_strenght_area_name_3, 
        t.user_strenght_area_name_4, 
        t.user_development_area_name_1, 
        t.user_development_area_name_2, 
        t.user_development_area_name_3, 
        t.line_manager_strenght_area_name_1, 
        t.line_manager_strenght_area_name_2, 
        t.line_manager_strenght_area_name_3, 
        t.line_manager_strenght_area_name_4, 
        t.line_manager_strenght_area_comment_1, 
        t.line_manager_strenght_area_comment_2, 
        t.line_manager_strenght_area_comment_3, 
        t.line_manager_strenght_area_comment_4, 
        t.line_manager_development_area_name_1, 
        t.line_manager_development_area_name_2, 
        t.line_manager_development_area_name_3, 
        t.line_manager_development_area_comment_1, 
        t.line_manager_development_area_comment_2, 
        t.line_manager_development_area_comment_3, 
        t.line_manager_final_comment, 
        t.line_manager_step_status, 
        t.task_closing_date, 
        t.company_profile_id

    FROM corpname_skill o
    RIGHT JOIN corpname_skill_new t
    ON t.company_profile_id = o.company_profile_id
    WHERE o.company_profile_id IS NULL
EXCEPTION 
    WHEN SQLSTATE '23505' THEN RAISE NOTICE 'skipped row';
END;

然而错误没有被捕获,我一直遇到同样的错误。我也尝试使用ON CONFLICT子句而不是这个,但是我遇到了语法错误。 我想要实现的是一种跳过此错误并继续INSERT的方法,因此在这种情况下跳过导致错误的行。

对此有何帮助?

1 个答案:

答案 0 :(得分:0)

您似乎将SQL statement BEGIN(开始交易)与BEGIN and EXCEPTION keywordsblock-structured procedural language PL/pgSQL混淆。

普通SQL中没有EXCEPTION命令。

要处理INSERT操作中的重复键错误,ON CONFLICT子句将是您的最佳选择。基本语法:

INSERT ... ON CONFLICT DO NOTHING;

详细说明: