From: Donald Fraser <postgres@kiwi-fraser.net>
To: [ADMIN] <pgsql-admin@postgresql.org>
Subject: Re: How to enforce the use of the sequence for serial columns ?
Date: Wed, 13 Dec 2006 12:08:59 -0000
Message-ID: <004f01c71eaf$790c5570$7d64a8c0@demolish1> (raw)
References: <CA896D7906BF224F8A6D74A1B7E54AB319875F@JENMAIL01.ad.intershop.net>
How to enforce the use of the sequence for serial columns ?There are many
ways, here are a couple to think about:
1)
Revoke all access to the table and then create a VIEW to access the table
where by you simply don't use column "id" in the VIEWs insert / update
statements.
2)
Manually retrieve the serial number from a trigger function and modify
column "id" in the function.
Regards
Donald Fraser
----- Original Message -----
From: Marc Mamin
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 ?
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
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: postgres@kiwi-fraser.net
Subject: Re: How to enforce the use of the sequence for serial columns ?
In-Reply-To: <004f01c71eaf$790c5570$7d64a8c0@demolish1>
References: <CA896D7906BF224F8A6D74A1B7E54AB319875F@JENMAIL01.ad.intershop.net> <004f01c71eaf$790c5570$7d64a8c0@demolish1>
* 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