简体   繁体   English

错误:违反外键约束,父表中不存在键(但它是??)

[英]ERROR: violates foreign key constraint, key is not present in parent table (but it is??)

I know this question has been asked many times, but none of the answers have solved my issue.我知道这个问题已经被问过很多次了,但没有一个答案能解决我的问题。

I am creating a database for a uni assignment, using PostgreSQL through pgadmin 4, and I have a table named "staff" populated with staff members with a primary key of "staffid".我正在为 uni 任务创建一个数据库,通过 pgadmin 4 使用 PostgreSQL,我有一个名为“staff”的表,其中填充了主键为“staffd”的员工。 I then have another table named "client_international", which includes a foreign key of "staffid" which relates to the staff tables primary key.然后我有另一个名为“client_international”的表,其中包含一个与员工表主键相关的“员工”外键。

When trying to insert into the client table, I am getting the following error:尝试插入客户端表时,出现以下错误:

ERROR: insert or update on table "client_international" violates foreign key constraint "intclient_staff_fkey"错误:插入或更新表“client_international”违反外键约束“intclient_staff_fkey”
DETAIL: Key (staffid)=(100000024) is not present in table "staff".详细信息:键 (staff)=(100000024) 不存在于表“staff”中。
SQL state: 23503 SQL 状态:23503

I am certain that that '100000024' key is in the staff table.. yet I still get the error.我确信“100000024”键在员工表中......但我仍然收到错误消息。 Any suggestions?有什么建议? Below I will paste the code I used to create the staff and client tables, in case anyone notices an error in them.下面我将粘贴我用来创建员工表和客户表的代码,以防有人注意到其中的错误。

Staff table:员工表:

CREATE SEQUENCE staff_seq
    start 100000000
    increment 1;

CREATE TABLE staff 
(
    staffid integer default nextval('staff_seq'),
    firstname varchar(20) NOT NULL,
    lastname varchar(20) NOT NULL,
    "position" varchar(20) NOT NULL,
    mobile varchar(20) NOT NULL,
    email varchar(100) NOT NULL,
    "location" integer NOT NULL,
    CONSTRAINT staff_pkey PRIMARY KEY (staffid)
);

Client table:客户表:

CREATE SEQUENCE client_seq
    start 200000000
    increment 1;

CREATE TABLE client  
(
    clientid integer default nextval('client_seq'),
    company varchar(100) NOT NULL,
    sector varchar(100) NOT NULL,
    pointofcontact varchar(20) NOT NULL,
    mobile varchar(20) NOT NULL,
    email varchar(100) NOT NULL,
    approvalstatus boolean default (false),
    "location" integer NOT NULL,
    staffid integer NOT NULL,
    CONSTRAINT client_pkey PRIMARY KEY (clientid)
);

CREATE TABLE client_international 
(
    CONSTRAINT client_international_pkey PRIMARY KEY (clientid)
) INHERITS ("client");

ALTER TABLE client
ADD CONSTRAINT client_location_fkey FOREIGN KEY ("location") REFERENCES "location" (locationid),
ADD CONSTRAINT client_staff_fkey FOREIGN KEY (staffid) REFERENCES staff (staffid);

ALTER TABLE client_international
ADD CONSTRAINT intclient_location_fkey FOREIGN KEY ("location") REFERENCES "location" (locationid),
ADD CONSTRAINT intclient_staff_fkey FOREIGN KEY (staffid) REFERENCES staff (staffid);

I get the error when running the following statements:运行以下语句时出现错误:

INSERT INTO client_international(company, sector, pointofcontact, mobile, email, approvalstatus, "location", staffid)
VALUES  ('Moores Dogs', 'Border Patrol', 'Carol Moore', '07911 653453', 'jenkinsj@k9solutions.co.uk', 'false', '500000001', '100000024');

Here's a screenshot of the entry in the staff table, showing that it's definitely in there:这是员工表中条目的屏幕截图,显示它肯定在那里:

图片

Foreign keys aren't "inherited".外键不是“继承的”。

Quote from the manual 从手册中引用

A serious limitation of the inheritance feature is that [...] foreign key constraints only apply to single tables, not to their inheritance children .继承功能的一个严重限制是 [...] 外键约束仅适用于单个表,而不适用于它们的继承子表 This is true on both the referencing and referenced sides of a foreign key constraint.在外键约束的引用侧和被引用侧都是如此。

(emphasis mine) (强调我的)

So what you are trying to do, simply isn't supported.因此,您正在尝试做的事情根本不受支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果违反外键约束,如何插入到表中 - How to insert into a table if it violates a foreign key constraint 在表上插入或更新违反外键约束 - insert or update on table violates foreign key constraint 在表“员工”上进行更新或删除违反了外键约束 - update or delete on table “employee” violates foreign key constraint 表上的插入或更新违反了 Postgres 的外键约束 - Insert or Update on Table Violates Foreign Key Constraint with Postgres POSTGRESQL。 在表上插入或更新违反外键约束 - POSTGRESQL. Insert or update on table violates foreign key constraint 在GO中,对表“ tablename”的更新或删除违反外键约束时,此错误的名称是什么? - What is the name of this error `update or delete on table “tablename” violates foreign key constraint` in GO? postgresql和Delete语句违反了外键约束 - postgresql and Delete statement violates foreign key constraint 更新或删除违反外键约束 - Update or Delete Violates foreign key constraint Hibernate / JPA-删除具有@OneToMany关系的表(一个父级-0或多个相同类型的子级)违反了外键约束 - Hibernate/JPA - Delete table with @OneToMany relationship (one parent- 0 or more child of the same type) violates foreign key constraint 为什么在将 2 个表的关系输入到关系表中时出现外键约束错误? - Why am I getting a foreign key constraint error when entering a relation of 2 tables with data present into a relationship table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM