pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ben K. <bkim@coe.tamu.edu>
To: pgsql-admin@postgresql.org
Subject: Re: How to enforce the use of the sequence for serial columns
Date: Wed, 13 Dec 2006 08:54:22 -0600 (CST)
Message-ID: <Pine.GSO.4.64.0612130844060.5840@coe.tamu.edu> (raw)

"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






view thread (9+ messages)  latest in thread

Message-ID: <Pine.GSO.4.64.0612130844060.5840@coe.tamu.edu>
Permalink:  ../Pine.GSO.4.64.0612130844060.5840@coe.tamu.edu/
Also on:    postgresql.org/message-id/Pine.GSO.4.64.0612130844060.5840@coe.tamu.edu

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: bkim@coe.tamu.edu
  Subject: Re: How to enforce the use of the sequence for serial columns
  In-Reply-To: <Pine.GSO.4.64.0612130844060.5840@coe.tamu.edu>
  References: <Pine.GSO.4.64.0612130844060.5840@coe.tamu.edu>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox