Re: How to enforce the use of the sequence for serial columns

From: "Ben K(dot)" <bkim(at)coe(dot)tamu(dot)edu>
To: pgsql-admin(at)postgresql(dot)org
Subject: Re: How to enforce the use of the sequence for serial columns
Date: 2006-12-13 14:54:22
Message-ID: Pine.GSO.4.64.0612130844060.5840@coe.tamu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

"Marc Mamin" <M(dot)Mamin(at)intershop(dot)de> writes:
> I'd like to ensure that nobody provide the ID in an insert statement
> when the id is linked to a sequence.
> I tried it with a trigger, but the id value is fed before the "BEFORE
> INSERT" test is performed (see below)...

If you have the luxury of re-creating the table (dump, edit schema,
reload), you might want to try this:

create table mytable (myid serial primary key constraint mytable_myid_chk check (myid =
currval('mytable_myid_seq'), mydata varchar(255), ...);

This will prevent "serial" id field from being arbitrarily manipulated
outside the sequence..

There may be an equivalent way using alter table, but on a first look, currval
cannot be used to check existing values.

Regards

Ben K.
Developer
http://benix.tamu.edu

Browse pgsql-admin by date

  From Date Subject
Next Message Antonios Katsikadamos 2006-12-13 17:10:33 storage
Previous Message Jerry Sievers 2006-12-13 14:08:26 Re: How to enforce the use of the sequence for serial columns ?