From: Sam Barnett-Cormack <s.barnett-cormack@lancaster.ac.uk>
To: CHRIS HOOVER <CHRIS.HOOVER@companiongroup.com>
Cc: pgsql-admin@postgresql.org
Subject: Re: Help with foreign key creation problem
Date: Thu, 13 May 2004 15:52:04 +0100 (BST)
Message-ID: <Pine.LNX.4.58.0405131551230.2878@localhost.localdomain> (raw)
In-Reply-To: <NXcdf526-39ff0515@companiongroup.com>
References: <NXcdf526-39ff0515@companiongroup.com>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: s.barnett-cormack@lancaster.ac.uk, CHRIS.HOOVER@companiongroup.com
Subject: Re: Help with foreign key creation problem
In-Reply-To: <Pine.LNX.4.58.0405131551230.2878@localhost.localdomain>
References: <NXcdf526-39ff0515@companiongroup.com> <Pine.LNX.4.58.0405131551230.2878@localhost.localdomain>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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