Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 92F7B9FB989 for ; Wed, 28 Mar 2007 12:33:45 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 86700-04 for ; Wed, 28 Mar 2007 12:33:31 -0300 (ADT) X-Greylist: delayed 00:09:34.426992 by SQLgrey-1.7.4 Received: from n054.sc0.he.tucows.com (smtpout1099.sc0.he.tucows.com [64.97.144.99]) by postgresql.org (Postfix) with ESMTP id 60FDF9FB94D for ; Wed, 28 Mar 2007 12:33:27 -0300 (ADT) Received: from [192.168.168.5] (74.94.66.41) by n054.sc0.he.tucows.com (7.2.069.1) (authenticated as ray@madigans.org) id 460994360001F764 for pgsql-sql@postgresql.org; Wed, 28 Mar 2007 15:23:47 +0000 Message-ID: <460A96ED.9000001@madigans.org> Date: Wed, 28 Mar 2007 08:25:17 -0800 From: Ray Madigan User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Pgsql-Sql Subject: Rules with sequence columns Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/335 X-Sequence-Number: 27973 I have the following situation that I would appreciate your input on: I have a table with a column that I use to uniquely identify its rows. The table also has a rule on insert that stores the row identifier into another table for reference at some other point. The table is defined as CREATE SEQUENCE foo_seq; CREATE TABLE foo ( fooK INTEGER DEFAULT NEXTVAL ( 'foo_seq' ), fooN VARCHAR(32) NOT NULL UNIQUE, link INTEGER NOT NULL DEFAULT 0 ); The rule does an insert into another table and I have implemented the rule in two ways. CREATE RULE insertCD AS ON INSERT TO foo DO INSERT INTO cdFoo ( contextK, componentK ) SELECT currval ( 'foo_seq' ), componentK FROM Component WHERE componentN = 'Division'; or CREATE RULE insertCD AS ON INSERT TO foo DO INSERT INTO cdFoo ( contextK, componentK ) SELECT new.fooK, componentK FROM Component WHERE componentN = 'Division'; The situation is that every time the rule fires, the foo sequence is incremented for each row in the foo table. and the reference value is not the same in the table. I have tried to take the default nextval ( 'foo_seq' ) from the row initialization and move it to the insert insert into foo ( fook, fooN ) values ( nextval ( 'foo_seq' ), 'Name' ); with the same result. The only way I have been able to make it work is ugly. int fooK = select nextval ( 'foo_seq' ); insert into foo ( fooK, fooN ) values ( fooK, 'Name' ); Does anyone have any suggestion?