Re: Multiple Foreign Keys to same table and field

From: "Robert Fitzpatrick" <robert(at)webtent(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Multiple Foreign Keys to same table and field
Date: 2002-12-24 00:06:42
Message-ID: 002601c2aae0$5a2e3890$bbc2fea9@COLUMBUS
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> "Robert Fitzpatrick" <robert(at)webtent(dot)com> writes:
> > Is it possible to have multiple Foreign Keys in one table
> on different
> > fields related to the same field in one other table?
>
> From: Tom Lane [mailto:tgl(at)sss(dot)pgh(dot)pa(dot)us]
> Offhand I think that should work. Could we see a complete
> example of what's going wrong for you?
>
> regards, tom lane
>

Thanks for the quick response. Here is what I did to create all the
tables:

CREATE TABLE "table1" (
"field1" varchar(10) NOT NULL PRIMARY KEY, "field2" char(12), "field3"
char(12)
)

CREATE TABLE "table2" (
field1 char(12) NOT NULL PRIMARY KEY, field2 char(12)
)

ALTER TABLE "table1" ADD CONSTRAINT "field2_table2_field1" FOREIGN KEY
("field2") REFERENCES "table2"("field1") ON DELETE CASCADE ON UPDATE
CASCADE NOT DEFERRABLE;

ALTER TABLE "table1" ADD CONSTRAINT "field3_table2_field1" FOREIGN KEY
("field3") REFERENCES "table2"("field1") ON DELETE CASCADE ON UPDATE
CASCADE NOT DEFERRABLE;

INSERT INTO "table2" ("field1", "field2") VALUES ('test', 'test')

INSERT INTO "table1" ("field1", "field2", "field3") VALUES ('test',
'test', 'test')

Now, I do the update and the resulting error message:

UPDATE "table2" SET "field1" = 'test1', "field2" = 'test ' WHERE
"field1" = 'test '

ERROR: field2_table2_field1 referential integrity violation - key
referenced from table1 not found in table2

--
Robert

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Lincoln Yeoh 2002-12-24 05:04:45 Re: SQL Injection & Stored Procedures Info
Previous Message Tom Lane 2002-12-23 23:21:52 Re: server closed the connectio unexpectedly