Re: How to enforce the use of the sequence for serial columns - Mailing list pgsql-admin

From Ben K.
Subject Re: How to enforce the use of the sequence for serial columns
Date
Msg-id Pine.GSO.4.64.0612130844060.5840@coe.tamu.edu
Whole thread Raw
List pgsql-admin
"Marc Mamin" <M.Mamin@intershop.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




pgsql-admin by date:

Previous
From: Jerry Sievers
Date:
Subject: Re: How to enforce the use of the sequence for serial columns ?
Next
From: Antonios Katsikadamos
Date:
Subject: storage