Re: Inheritance: problems with primary key - Mailing list pgsql-general

From Alvaro Herrera
Subject Re: Inheritance: problems with primary key
Date
Msg-id 20021004145237.GC2326@atentus.com
Whole thread Raw
In response to Inheritance: problems with primary key  (camilarrocha@hotmail.com (Camila Rocha))
List pgsql-general
On Thu, Oct 03, 2002 at 05:31:52AM -0700, Camila Rocha wrote:

> I'm having troubles with inheritance too. I've created a table,
> "person", and it has a field "code" as its primary key.
> After, I've created a table, "student", that inherits person. I wanted
> too use the column person.code as the primary key in the students
> table too, but I've noticed that, when I insert a student, the
> uniqueness of the primary key is no granted. for example, I can insert
> two students.code = "1"

One way to help the insertion but that will not enforce the primary key
uniqueness is to add a default value to students.code that refers to the
sequence that was created for person.code, if you are using a serial
type.

Sadly, there's no way to enforce uniqueness across tables; this issues
are being discussed right now.  For now, the way to enforce it is to
create a separate table with one column (code UNIQUE) and have a trigger
to insert the value of the primary key from each table in the
inheritance tree in that table.

--
Alvaro Herrera (<alvherre[a]atentus.com>)
"¿Qué importan los años?  Lo que realmente importa es comprobar que
a fin de cuentas la mejor edad de la vida es estar vivo"  (Mafalda)

pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: Rows ordered
Next
From: Tom Lane
Date:
Subject: Re: Performing INSERT from a Trigger fired function