pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: CHRIS HOOVER <CHRIS.HOOVER@companiongroup.com>
To: pgsql-admin@postgresql.org
Subject: Help with foreign key creation problem
Date: Thu, 13 May 2004 10:34:00 -0400
Message-ID: <NXcdf526-39ff0515@companiongroup.com> (raw)

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



view thread (3+ messages)  latest in thread

Message-ID: <NXcdf526-39ff0515@companiongroup.com>
Permalink:  ../NXcdf526-39ff0515@companiongroup.com/
Also on:    postgresql.org/message-id/NXcdf526-39ff0515@companiongroup.com

 · 

reply

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: CHRIS.HOOVER@companiongroup.com
  Subject: Re: Help with foreign key creation problem
  In-Reply-To: <NXcdf526-39ff0515@companiongroup.com>
  References: <NXcdf526-39ff0515@companiongroup.com>

* 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