Thread: Inheritance: problems with primary key

Inheritance: problems with primary key

From
camilarrocha@hotmail.com (Camila Rocha)
Date:
hi,

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"

How can I inherit the primary key too??

thankx,
Camila

PS: Sorry about the bad english, i'm brazilian

Re: Inheritance: problems with primary key

From
Alvaro Herrera
Date:
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)