Re: Foreign Key - Mailing list pgsql-general

From Szymon Guz
Subject Re: Foreign Key
Date
Msg-id z2qe4edc9361004221532s79e484a7t465f6a1bf6e66746@mail.gmail.com
Whole thread Raw
In response to Foreign Key  ("Bob Pawley" <rjpawley@shaw.ca>)
List pgsql-general


2010/4/23 Bob Pawley <rjpawley@shaw.ca>
Hi
 
I don't understand what the following error means.
 
I am trying to add a foreign key to table image, that holds column device_id. This is to be controlled by column devices_id in table devices.
 
ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
 
ERROR:  insert or update on table "image" violates foreign key constraint "deviceidfk"
DETAIL:  Key (device_id)=(4022) is not present in table "devices".
 
Help appreciated.
 
Bob
Hi,
the error means that you try to insert some value to the column device_id in the image table that doesn't have a corresponding value in the devices table. If you want to insert something like this:

INSERT INTO image(device_id) VALUES ( 4022 );

there has to exist a record in the devices table that has the value 4022 in the corresponding column.

Oh, btw, the SQL command that you've shown doesn't add the foreign key on the image table.

regards
Szymon Guz

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: PSQL segmentation fault after setting host
Next
From: Tom Lane
Date:
Subject: Re: I/O error during autovacuum