DDL problems: Referential issue?

From: Leif Biberg Kristensen <leif(at)solumslekt(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: DDL problems: Referential issue?
Date: 2009-11-04 18:03:36
Message-ID: 200911041903.36563.leif@solumslekt.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

PostgreSQL 8.3.8 on Gentoo Linux.

I've got a junction table:

CREATE TABLE participants (
person_fk INTEGER REFERENCES persons (person_id),
event_fk INTEGER REFERENCES events (event_id) ON DELETE CASCADE,
sort_order INTEGER NOT NULL DEFAULT 1,
is_principal BOOLEAN NOT NULL DEFAULT TRUE,
PRIMARY KEY (person_fk, event_fk)
);
CREATE INDEX event_key ON participants (event_fk);
CREATE INDEX person_key ON participants (person_fk);

Now I want to add some text to a few participants, but as this will probably
only be for a few per cent, I try to create an extra table like this:

pgslekt=> CREATE TABLE participant_notes (
pgslekt(> person_fk INTEGER NOT NULL REFERENCES participants
(person_fk),
pgslekt(> event_fk INTEGER NOT NULL REFERENCES participants (event_fk)
ON DELETE CASCADE,
pgslekt(> part_note TEXT,
pgslekt(> PRIMARY KEY (person_fk, event_fk)
pgslekt(> );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"participant_notes_pkey" for table "participant_notes"
ERROR: there is no unique constraint matching given keys for referenced table
"participants"

I fail to see what is the problem. I even tried to add a unique constraint to
participants:

pgslekt=> alter table participants add constraint unique_person_event unique
(person_fk, event_fk);
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index
"unique_person_event" for table "participants"
ALTER TABLE

But I still get the same error message as above.

This works fine, naturally:

pgslekt=> CREATE TABLE participant_notes (
pgslekt(> person_fk INTEGER NOT NULL references persons (person_id),
pgslekt(> event_fk INTEGER NOT NULL references events (event_id) ON
DELETE CASCADE,
pgslekt(> part_note TEXT,
pgslekt(> PRIMARY KEY (person_fk, event_fk)
pgslekt(> );
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"participant_notes_pkey" for table "participant_notes"
CREATE TABLE

However, I think that this table should reference participants, not the
primary tables persons and events.
--
Leif Biberg Kristensen
http://solumslekt.org

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Scott Marlowe 2009-11-04 18:24:29 Re: DDL problems: Referential issue?
Previous Message Craig Ringer 2009-11-04 04:34:30 Re: Why don't I get a LATIN1 encoding here with SET ENCODING?