Re: Foreign Key

From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: Bob Pawley <rjpawley(at)shaw(dot)ca>
Cc: Postgresql <pgsql-general(at)postgresql(dot)org>
Subject: Re: Foreign Key
Date: 2010-04-22 22:32:43
Message-ID: z2qe4edc9361004221532s79e484a7t465f6a1bf6e66746@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

2010/4/23 Bob Pawley <rjpawley(at)shaw(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2010-04-22 22:47:33 Re: I/O error during autovacuum
Previous Message Tom Lane 2010-04-22 22:28:50 Re: PSQL segmentation fault after setting host