简体   繁体   English

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

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

I'm trying to run an INSERT query on TablePlus .我正在尝试在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;

Instead of the query sending a success message, I'm getting an ERROR: constraint "minutes_clone_stick_tickers_unique" for table "minutes_clone" does not exist.而不是查询发送成功消息,我收到一个错误:表“minutes_clone”的约束“minutes_clone_stick_tickers_unique”不存在。

Here is an image of my table structure.是我的表结构的图像。

to replicate:复制:

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");

I've tried many things, like removing ON CONSTRAINT and dropping and re-adding the constraing but haven't been able to solve this issue.我尝试了很多事情,例如删除ON CONSTRAINT并删除并重新添加约束,但无法解决此问题。 Any solutions?任何解决方案?

You are mixing up indexes and constraints.您正在混淆索引和约束。 That is understandable, because a unique constraint is always implemented by a unique index, but they are still not the same.这是可以理解的,因为唯一约束总是由​​唯一索引实现,但它们仍然不一样。

To make your statement work, you need a unique constraint on top of the index you currently have.为了使您的语句起作用,您需要在当前拥有的索引之上添加一个唯一约束。 You can create that with:您可以使用以下方法创建它:

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