关于sql:ERROR:违反外键约束,父表中不存在键(但它是??)

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

我知道这个问题已经问过很多遍了,但是没有一个答案能解决我的问题。

我正在通过pgadmin 4使用PostgreSQL创建一个用于uni分配的数据库,并且我有一个名为" staff"的表,该表中填充有工作人员且主键为" staffid"。然后,我还有另一个名为" client_international"的表,其中包含与员工表主键相关的" staffid"外键。

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

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'键在人员表中。有什么建议么?在下面,我将粘贴用于创建人员表和客户表的代码,以防任何人注意到它们中的错误。

工作人员表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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)
);

客户表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);

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

1
2
INSERT INTO client_international(company, sector, pointofcontact, mobile, email, approvalstatus,"location", staffid)
VALUES  ('Moores Dogs', 'Border Patrol', 'Carol Moore', '07911 653453', '[email protected]', 'false', '500000001', '100000024');

这是人员表中条目的屏幕截图,显示该条目肯定在其中:

Image


外键不是"继承"的。

手册引用

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.

(强调我的)

因此,您尝试执行的操作完全不受支持。