Re: Can't insert date in field with foreign key

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: bjoern(dot)platzen(at)momatec(dot)de
Cc: "PostgreSQL List" <pgsql-general(at)postgresql(dot)org>, Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
Subject: Re: Can't insert date in field with foreign key
Date: 2004-11-11 15:59:45
Message-ID: 8485.1100188785@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

=?iso-8859-1?Q?Bj=F6rn_Platzen?= <bjoern(dot)platzen(at)momatec(dot)de> writes:
> CREATE TABLE "T_OEPNV_HST"
> (
> "HST_ID" serial,
> "HST_NR" varchar(10),
> ...
> CONSTRAINT "T_OEPNV_HST_pkey" PRIMARY KEY ("HST_ID"),
> CONSTRAINT "T_OEPNV_HST_unr" UNIQUE ("HST_NR"),
> ...
> CREATE TABLE "T_OEPNV_HST_LIN" (
> "HST_NR" varchar(10) references "T_OEPNV_HST" on update cascade,
> );

> So, I can't understand, why I get the error, that I can't insert the
> data into "T_OEPNV_HST_LIN" because there is no HST_NR='10001' in
> "T_OEPNV_HST".

Since you wrote the REFERENCES clause without mentioning any particular
column name, it defaults to referencing the primary key of
"T_OEPNV_HST", that is, "HST_ID".

When I try the example I get

ERROR: insert or update on table "T_OEPNV_HST_LIN" violates foreign key constraint "T_OEPNV_HST_LIN_HST_NR_fkey"
DETAIL: Key (HST_NR)=(10001) is not present in table "T_OEPNV_HST".

The error message is mentioning the referencing column not the
referenced column. I recall that we decided this was less confusing
than the other choice, but I'm not sure why we thought that.
Particularly now that the FK constraint name includes the referencing
column name by default, I wonder if we ought to switch.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Phil Endecott 2004-11-11 16:50:56 Re: Analyse - max_locks_per_transaction - why?
Previous Message David Parker 2004-11-11 15:47:59 logging