查找错误原因"约束xxxx不是外键约束"在Postgresql中

时间:2017-08-27 07:51:54

标签: postgresql

我已经定义了这些表:

CREATE TABLE "public".category (id BIGSERIAL NOT NULL, name varchar(255) NOT NULL, PRIMARY KEY (id));
CREATE UNIQUE INDEX category_name ON "public".category (name);

CREATE TABLE "public".clusters (id BIGSERIAL NOT NULL, name varchar(255) NOT NULL, PRIMARY KEY (id));
CREATE INDEX clusters_name ON "public".clusters (name);

CREATE TABLE "public".keywords (id BIGSERIAL NOT NULL, text varchar(255) NOT NULL, category_id int8 NOT NULL, top_results int4, cluster_id int8, month_requests int4, click_cost float8, PRIMARY KEY (id));
CREATE INDEX keywords_text ON "public".keywords (text);
ALTER TABLE "public".keywords ADD CONSTRAINT FKkeywords488682 FOREIGN KEY (cluster_id) REFERENCES "public".clusters (id);
ALTER TABLE "public".keywords ADD CONSTRAINT FKkeywords446526 FOREIGN KEY (category_id) REFERENCES "public".category (id) ON UPDATE CASCADE ON DELETE CASCADE;

在类别表中添加了一条记录:

INSERT INTO "public".category(id, name) VALUES (1, 'Test');

现在当我尝试将记录添加到关键字表

insert into "public"."keywords" ( "category_id", "text") values ( 1, 'testkey')

我收到了错误:

  

错误:约束16959不是外键约束

当我这样做时

select * FROM pg_constraint;

我无法看到此ID的约束。我无法理解这个问题的原因是什么。

0 个答案:

没有答案