gpt4 book ai didi

sql - PostgreSQL 外键约束找不到现有键

转载 作者:行者123 更新时间:2023-11-29 13:30:31 25 4
gpt4 key购买 nike

我有这张表:

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 补丁版本,请立即升级并 REINDEX。如果是,我会检查机器上的内存问题、最近的 MCE(机器检查异常)、查找存储错误消息、检查磁盘等。

如果查询产生相同的结果,那么您的表格内容或定义与您想象的不同。

关于sql - PostgreSQL 外键约束找不到现有键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24793273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com