簡體   English   中英

PostgreSQL 上不存在表 t 的約束 c,即使它存在

[英]constraint c for table t does not exist on PostgreSQL even though it's there

我正在嘗試在TablePlus上運行 INSERT 查詢。

INSERT INTO minutes_clone (date, ticker, "lastTime", "openTime", date_time, group_type, "totalVolume", "totalPrice", "totalTrades")
        VALUES('2021-07-02', 'YELP', '00:15:00', '00:00:00', '2021-07-02 00:00:00', 15,  0,  0,  0) 
        ON CONFLICT ON CONSTRAINT minutes_clone_stick_tickers_unique 
        DO UPDATE SET "lastTime" = '00:15:00', "openTime" = '00:00:00', date_time = '2021-07-02 00:00:00', "totalVolume" = 0, "totalPrice" = 0, "totalTrades" = 0
        RETURNING id;

而不是查詢發送成功消息,我收到一個錯誤:表“minutes_clone”的約束“minutes_clone_stick_tickers_unique”不存在。

是我的表結構的圖像。

復制:

CREATE TABLE "public"."minutes_clone" (
    "ticker" varchar NOT NULL,
    "totalTrades" int4 NOT NULL,
    "totalPrice" numeric NOT NULL,
    "totalVolume" int4,
    "lastTime" time NOT NULL,
    "openTime" time NOT NULL,
    "date" date NOT NULL,
    "group_type" int4 NOT NULL DEFAULT 1,
    "date_time" timestamp,
    "parent_id" int4,
    "id" int4 NOT NULL DEFAULT nextval('id_seq'::regclass),
    PRIMARY KEY ("id")
);

CREATE INDEX "minutes_clone_ticker_group_date_index" ON "public"."minutes_clone" USING BTREE ("ticker","group_type","date_time");

CREATE UNIQUE INDEX "minutes_clone_stick_tickers_unique" ON "public"."minutes_clone" USING BTREE ("date","ticker","openTime","group_type");

CREATE INDEX "minutes_clone_date_time_index" ON "public"."minutes_clone" USING BTREE ("date_time");

我嘗試了很多事情,例如刪除ON CONSTRAINT並刪除並重新添加約束,但無法解決此問題。 任何解決方案?

您正在混淆索引和約束。 這是可以理解的,因為唯一約束總是由​​唯一索引實現,但它們仍然不一樣。

為了使您的語句起作用,您需要在當前擁有的索引之上添加一個唯一約束。 您可以使用以下方法創建它:

ALTER TABLE public.minutes_clone
   ADD UNIQUE USING INDEX minutes_clone_stick_tickers_unique;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM