![](/img/trans.png)
[英]Table variable doesn't exist even though it clearly defined in PostgreSQL
[英]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.