Re: table inheritance and foreign key troubles

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Kevin Way <kevin(dot)way(at)overtone(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: table inheritance and foreign key troubles
Date: 2001-09-11 10:58:14
Message-ID: Pine.BSF.4.21.0109110349380.20489-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


(hopefully this doesn't double post... stupid mail system)

On Tue, 11 Sep 2001, Christof Glaser wrote:

> On Tuesday, 11. September 2001 10:04, Kevin Way wrote:
> > I'm having a little trouble with some inherited tables and a foreign
> > key. Here's a simplified case, to show the trouble.
> >
> > CREATE TABLE node (
> > node_id SERIAL NOT NULL,
> > name TEXT NOT NULL,
> > PRIMARY KEY (node_id)
> > );
> > -- works just fine
> >
> > CREATE TABLE users (
> > email TEXT NOT NULL
> > ) INHERITS (node);
> > -- so far so good....
> >
> > CREATE TABLE item (
> > reason TEXT NOT NULL,
> > author_id INT NOT NULL REFERENCES users (node_id)
> > ) INHERITS (node);
> > ERROR: UNIQUE constraint matching given keys for referenced table
> > "users" not found
>
> That means, there is no UNIQUE constraing on users.node_id ;-)
> Since users inherits that field from node, just make node.node_id
> unique, or even a primary key.

Actually node.node_id looks to be a pkey, but primary keys/unique don't
inherit, so users.node_id doesn't have the constraint. You'll need
a primary key(node_id) on users as well (note that this won't actually
enforce that values are unique across both node and users just within
each table. See past discussions about inheritance and foreign keys...

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Adam Lang 2001-09-11 12:52:57 Re: SQL Query
Previous Message Christof Glaser 2001-09-11 10:49:13 Re: table inheritance and foreign key troubles