Re: Inheritance: problems with primary key

From: Alvaro Herrera <alvherre(at)atentus(dot)com>
To: Camila Rocha <camilarrocha(at)hotmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Inheritance: problems with primary key
Date: 2002-10-04 14:52:37
Message-ID: 20021004145237.GC2326@atentus.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2002-10-04 14:54:31 Re: Rows ordered
Previous Message Ex Fed 2002-10-04 14:49:01 Distributed / Linked with Open Source DB