pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jerry Sievers <jerry@jerrysievers.com>
To: Marc Mamin <M.Mamin@intershop.de>
Cc: pgsql-admin@postgresql.org
Subject: Re: How to enforce the use of the sequence for serial columns ?
Date: 13 Dec 2006 09:08:26 -0500
Message-ID: <m3mz5rkhwl.fsf@homie.jerrysievers.com> (raw)
In-Reply-To: <CA896D7906BF224F8A6D74A1B7E54AB319875F@JENMAIL01.ad.intershop.net>
References: <CA896D7906BF224F8A6D74A1B7E54AB319875F@JENMAIL01.ad.intershop.net>

"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)...
> 
> 
> Any Idea ?

Trigger based solution where same trig can be used for any number of
tables by changing the parameter.

Will throw one of 2 exceptions on failure to use sequence for the insert.

create table foo (a serial);
psql:q:2: NOTICE:  CREATE TABLE will create implicit sequence "foo_a_seq" for serial column "foo.a"
CREATE TABLE
create function footrig()
returns trigger
as $$
begin
	-- may throw currval not defined exception
	if new.a = currval(tg_argv[0]) then
		-- currval defined and equal new col value
		return new;		       
	end if;

	-- currval is defined but not used for this insert
	raise exception 'Not using default sequence';
end
$$ language plpgsql;
CREATE FUNCTION
create trigger footrig
before insert on foo
for each row
execute procedure footrig('foo_a_seq');
CREATE TRIGGER
You are now connected to database "jerry".
insert into foo values (1000);
psql:q:25: ERROR:  currval of sequence "foo_a_seq" is not yet defined in this session
CONTEXT:  PL/pgSQL function "footrig" line 3 at if
insert into foo values (default);
INSERT 0 1
insert into foo values (1000);
psql:q:27: ERROR:  Not using default sequence
select * from foo;
 a 
---
 1
(1 row)


> Cheers,
> 
> Marc
> 
> 
> 
> CREATE FUNCTION serialtest() RETURNS trigger AS $serialtest$
>     BEGIN
>         -- Check that the id is provided
>         IF NEW.id IS NOT NULL THEN
>             RAISE EXCEPTION 'id will be set from a sequence; do not
> provide it!';
>         END IF;
>         
>         RETURN NEW;
>     END;
> $serialtest$ LANGUAGE plpgsql;
> 
> 
> CREATE TABLE test_table
> (
>   id serial primary key,
>   foo int
> );
> 
> 
> CREATE TRIGGER test BEFORE INSERT OR UPDATE ON test_table     
> FOR EACH ROW EXECUTE PROCEDURE serialtest();
> 
> 
> insert into test_table(foo)values(1);
> 
> ERROR: id will be set from a sequence; do not provide it!
> SQL state: P0001

-- 
-------------------------------------------------------------------------------
Jerry Sievers   305 854-3001 (home)     Production Database Administrator
                305 321-1144 (mobil	WWW E-Commerce Consultant



view thread (9+ messages)  latest in thread

Message-ID: <m3mz5rkhwl.fsf@homie.jerrysievers.com>
Permalink:  ../m3mz5rkhwl.fsf@homie.jerrysievers.com/
Also on:    postgresql.org/message-id/m3mz5rkhwl.fsf@homie.jerrysievers.com

 · 

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: jerry@jerrysievers.com, M.Mamin@intershop.de
  Subject: Re: How to enforce the use of the sequence for serial columns ?
  In-Reply-To: <m3mz5rkhwl.fsf@homie.jerrysievers.com>
  References: <CA896D7906BF224F8A6D74A1B7E54AB319875F@JENMAIL01.ad.intershop.net> <m3mz5rkhwl.fsf@homie.jerrysievers.com>

* 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