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

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

我知道这个问题已经被问过很多次了,但是 none 的答案已经解决了我的问题。

我正在为 uni 作业创建一个数据库,通过 pgadmin 4 使用 PostgreSQL,我有一个名为 "staff" 的 table,其中填充了主键为 "staffid" 的工作人员.然后我有另一个名为 "client_international" 的 table,它包含一个 "staffid" 的外键,它与员工 table 的主键相关。

尝试插入客户端 table 时,出现以下错误:

ERROR: insert or update on table "client_international" violates foreign key constraint "intclient_staff_fkey"
DETAIL: Key (staffid)=(100000024) is not present in table "staff".
SQL state: 23503

我确定“100000024”键在工作人员 table 中。但我仍然遇到错误。有什么建议么?下面我将粘贴我用来创建员工和客户的代码 tables,以防有人注意到其中的错误。

员工 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)
);

客户端 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);

当 运行 以下语句时出现错误:

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');

这是工作人员 table 中条目的屏幕截图,显示它肯定在那里:

外键不是 "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.

(强调我的)

所以您尝试做的事情根本不受支持。