psycopg2.NotSupportedError: INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules

psycopg2.NotSupportedError: INSERT with ON CONFLICT clause cannot be used with table that has INSERT or UPDATE rules

当我在 CONFLICT 子句旁边创建更新规则时,就会抛出该错误。

这是我的冲突代码

 insert_query = "INSERT INTO my_company (id, name, login, logout) VALUES %s\
                    ON CONFLICT (id) DO NOTHING"

我的更新规则

CREATE RULE log_shoelace AS ON UPDATE TO my_company
    WHERE NEW.login <> OLD.login or NEW.logout <> OLD.logout
    DO INSERT INTO my_company VALUES (
    new.id, new.name, new.login, new.logout, new.interval_time, current_date);

my_company table字段包含id、name、login。注销,interval_time,今天。

如果有任何数据更新,则将这些数据插入相同的 table。 但是在这里我不能同时使用 CONFLICT 和 RULE。那么在这种情况下我能做什么呢?

谢谢。

Table 用于测试的创建和序列创建:

CREATE SEQUENCE IF NOT EXISTS my_company_id_seq;

CREATE TABLE public.my_company
( id            integer NOT NULL DEFAULT nextval('my_company_id_seq'::regclass)
, name          character varying(50)
, login         time without time zone
, logout        time without time zone
, interval_time time without time zone
, today         date DEFAULT CURRENT_DATE
, CONSTRAINT my_company_pkey PRIMARY KEY (id) 
);

ON CONFLICT 指令应该放在您的规则主体中:

CREATE RULE log_shoelace AS 
    ON UPDATE TO my_company
        WHERE NEW.login <> OLD.login or NEW.logout <> OLD.logout
    DO 
        INSERT INTO my_company VALUES (
            new.id, new.name, new.login, new.logout, new.interval_time, current_date)
    ON CONFLICT (id) DO NOTHING

说得好,ON CONFLICT 指令仅在目标 table 具有一些延迟冲突的约束时才起作用。否则,您将遇到运行时错误,例如:

No unique or exclusion constraint matching the ON CONFLICT specification.