PostgreSQL外键约束找不到现有键
我有这张桌子:PostgreSQL外键约束找不到现有键,sql,postgresql,postgresql-9.1,postgresql-9.2,postgresql-9.3,Sql,Postgresql,Postgresql 9.1,Postgresql 9.2,Postgresql 9.3,我有这张桌子: CREATE TABLE operation_history ( id bigserial NOT NULL, task_history bigint NOT NULL, operation smallint NOT NULL, CONSTRAINT operation_history_pkey PRIMARY KEY (id), CONSTRAINT operation_history_operation_fkey FOREIGN KEY (operatio
CREATE TABLE operation_history
(
id bigserial NOT NULL,
task_history bigint NOT NULL,
operation smallint NOT NULL,
CONSTRAINT operation_history_pkey PRIMARY KEY (id),
CONSTRAINT operation_history_operation_fkey FOREIGN KEY (operation)
REFERENCES desktop_operation (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT operation_history_task_history FOREIGN KEY (task_history)
REFERENCES task_history (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
我还有一个表task_history,其中有三行,ID为:1、2、3。但是,由于某种原因,当我在operation_history表上插入时,我得到一个错误,即task_history行不存在,并且没有我给它的id。以下是要插入的查询:
INSERT INTO operation_history (task_history, operation) VALUES
(1, 2);
下面是错误:
ERROR: insert or update on table "operation_history" violates foreign key constraint "operation_history_task_history"
DETAIL: Key (task_history)=(1) is not present in table "task_history".
不过,我可以肯定地知道,task_history表中有三行ID分别为1、2和3
是什么原因导致了这种情况?我如何补救?试试:
SET enable_seqscan = off;
SELECT t.id FROM task_history t WHERE t.id = 1;
SET enable_seqscan = on;
SET enable_indexscan = off;
SET enable_indexonlyscan = off;
SELECT t.id FROM task_history t WHERE t.id = 1;
如果结果不同,则可能是索引受损。这不应该发生。如果您不在当前的PostgreSQL修补程序版本上,请立即升级并重新索引。如果是,我将检查机器上的内存问题、最近的MCE(机器检查异常)、查找存储错误消息、检查磁盘等
如果查询产生相同的结果,那么您的表内容或定义就不是您认为的那样。非常不可能。。看到这把小提琴了吗