简体   繁体   中英

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". I then have another table named "client_international", which includes a foreign key of "staffid" which relates to the staff tables primary key.

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"
DETAIL: Key (staffid)=(100000024) is not present in table "staff".
SQL state: 23503

I am certain that that '100000024' key is in the staff table.. yet I still get the error. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM