微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何修复在 PostgreSQL 中创建外键的错误?

如何解决如何修复在 PostgreSQL 中创建外键的错误?

我在 Postgresql 数据库中有两个表:

CREATE TABLE anillamientos
(gbifid integer NOT NULL,specieskey integer,localidad integer)

CREATE TABLE sitios
(gid integer NOT NULL DEFAULT nextval('sitios_gid_seq'::regclass),nombre character varying(150) COLLATE pg_catalog."default",detalles character varying(100) COLLATE pg_catalog."default",CONSTRAINT sitios_pkey PRIMARY KEY (gid))

我想在第一个表中创建一个引用字段 gid 上的第二个的外键。我已经构建了以下代码

alter table anillamientos 
add constraint fk_anill_sitio 
foreign key (localidad) references sitios(gid);

但它返回以下错误

ERROR:  insert or update on table "anillamientos" violates foreign key constraint "fk_anill_sitio"
DETAIL:  Key (localidad)=(1) is not present in table "sitios".
sql state: 23503

这是什么意思?我已经检查过两个表中都存在 id 1。为什么不起作用,如何解决

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。