pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedHelp with foreign key creation problem
3+ messages / 2 participants
[nested] [flat]
* Help with foreign key creation problem
@ 2004-05-13 14:34 CHRIS HOOVER <CHRIS.HOOVER@companiongroup.com>
0 siblings, 1 reply; 3+ messages in thread
From: CHRIS HOOVER @ 2004-05-13 14:34 UTC (permalink / raw)
To: pgsql-admin
I need some help understanding and creating foreign keys in postgresql.
Here is an example of what I am trying to do:
I have table 1 with:
table1_id serial unique
table1_col1 varchar
I have table2 with:
table2_id serial unique
table1_id integer
table2_col1 varchar
When I try to create the foreign key with
alter table "schema_name"."table1"
add foreign key ("table1_id")
references "schema_name"."table2"("table1_id")
on delete cascade
on update cascade
not deferrable;
Postgres complains with:
ERROR: UNIQUE constraint matching given keys for referenced table "table2" not
found.
Why is postgresql demanding a unique key on table2.table1_id? It is a foreign
key in a parent/child 1 to many relationship.
Please help me understand what is going on, and what I am missunderstanding
about foreign keys.
Thanks
chris
Postgresql 7.3.4 on RH ES 2.1
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Help with foreign key creation problem
@ 2004-05-13 14:52 Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>
parent: CHRIS HOOVER <CHRIS.HOOVER@companiongroup.com>
0 siblings, 0 replies; 3+ messages in thread
From: Sam Barnett-Cormack @ 2004-05-13 14:52 UTC (permalink / raw)
To: CHRIS HOOVER <CHRIS.HOOVER@companiongroup.com>; +Cc: pgsql-admin
On Thu, 13 May 2004, CHRIS HOOVER wrote:
> I need some help understanding and creating foreign keys in postgresql.
>
> Here is an example of what I am trying to do:
>
> I have table 1 with:
> table1_id serial unique
> table1_col1 varchar
>
> I have table2 with:
> table2_id serial unique
> table1_id integer
> table2_col1 varchar
>
> When I try to create the foreign key with
> alter table "schema_name"."table1"
> add foreign key ("table1_id")
> references "schema_name"."table2"("table1_id")
> on delete cascade
> on update cascade
> not deferrable;
>
> Postgres complains with:
> ERROR: UNIQUE constraint matching given keys for referenced table "table2" not
> found.
>
> Why is postgresql demanding a unique key on table2.table1_id? It is a foreign
> key in a parent/child 1 to many relationship.
>
> Please help me understand what is going on, and what I am missunderstanding
> about foreign keys.
Only slightly, you're creating the wrong way around. You want to be
adding the foreign keep to table2.
--
Sam Barnett-Cormack
Software Developer | Student of Physics & Maths
UK Mirror Service (http://www.mirror.ac.uk) | Lancaster University
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Help with foreign key creation problem
@ 2004-05-13 15:05 CHRIS HOOVER <CHRIS.HOOVER@companiongroup.com>
0 siblings, 0 replies; 3+ messages in thread
From: CHRIS HOOVER @ 2004-05-13 15:05 UTC (permalink / raw)
To: Stephan Szabo <sszabo@megazone.bigpanda.com>; +Cc: pgsql-admin
You are absolutely right. I hate it when I get turned around. Thank you very
much for the assistance.
Chris
------------------( Forwarded letter 1 follows )---------------------
Date: Thu, 13 May 2004 07:57:38 -0700 (PDT)
To: chris.hoover
Cc: pgsql-admin@postgresql.org.comp
From: Stephan.Szabo[sszabo]@megazone.bigpanda.com.comp
Subject: Re: [ADMIN] Help with foreign key creation problem
On Thu, 13 May 2004, CHRIS HOOVER wrote:
> I need some help understanding and creating foreign keys in postgresql.
>
> Here is an example of what I am trying to do:
>
> I have table 1 with:
> table1_id serial unique
> table1_col1 varchar
>
> I have table2 with:
> table2_id serial unique
> table1_id integer
> table2_col1 varchar
>
> When I try to create the foreign key with
> alter table "schema_name"."table1"
> add foreign key ("table1_id")
> references "schema_name"."table2"("table1_id")
> on delete cascade
> on update cascade
> not deferrable;
>
> Postgres complains with:
> ERROR: UNIQUE constraint matching given keys for referenced table "table2" not
> found.
>
> Why is postgresql demanding a unique key on table2.table1_id? It is a foreign
> key in a parent/child 1 to many relationship.
Are you sure you're making the key the direction you want?
I would think you'd want a foreign key on table2(table1_id) referencing
table1(table1_id) not the other way around since presumably table1 is the
table with the authoratative list of table1_ids.
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2004-05-13 15:05 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-05-13 14:34 Help with foreign key creation problem CHRIS HOOVER <CHRIS.HOOVER@companiongroup.com>
2004-05-13 14:52 ` Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>
2004-05-13 15:05 Re: Help with foreign key creation problem CHRIS HOOVER <CHRIS.HOOVER@companiongroup.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox