Re: Help with foreign key creation problem

From: "CHRIS HOOVER" <CHRIS(dot)HOOVER(at)companiongroup(dot)com>
To: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: Help with foreign key creation problem
Date: 2004-05-13 15:05:00
Message-ID: NY07fb28-6b6c472b@companiongroup.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)postgresql(dot)org(dot)comp
From: Stephan(dot)Szabo[sszabo](at)megazone(dot)bigpanda(dot)com(dot)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.

Browse pgsql-admin by date

  From Date Subject
Next Message Laurens Wagemakers 2004-05-13 15:15:16 Re: GNUmakefile size 0
Previous Message Sam Barnett-Cormack 2004-05-13 14:52:04 Re: Help with foreign key creation problem