Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 6E7893405C8 for ; Wed, 22 Feb 2012 09:52:59 -0400 (AST) Received: from mgate.mtholyoke.edu ([138.110.1.180] helo=depot.mtholyoke.edu) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S0Ccr-0007gU-QQ for pgsql-sql@postgresql.org; Wed, 22 Feb 2012 13:52:58 +0000 Received: from smtp.mtholyoke.edu (smtp.mtholyoke.edu [138.110.1.184]) by depot.mtholyoke.edu (8.14.3/8.14.3/Debian-5+lenny1) with ESMTP id q1MDqeuf032419 for ; Wed, 22 Feb 2012 08:52:40 -0500 Received: from rock.mtholyoke.edu (rock.mtholyoke.edu [138.110.86.70]) by smtp.mtholyoke.edu (Postfix) with ESMTPS id 2F65D57DAC for ; Wed, 22 Feb 2012 08:52:40 -0500 (EST) Received: from rock.mtholyoke.edu (rpeterso@localhost [127.0.0.1]) by rock.mtholyoke.edu (8.14.3/8.14.3/Debian-9.4) with ESMTP id q1MDqd8P027333 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Wed, 22 Feb 2012 08:52:39 -0500 Received: (from rpeterso@localhost) by rock.mtholyoke.edu (8.14.3/8.14.3/Submit) id q1MDqdre027328 for pgsql-sql@postgresql.org; Wed, 22 Feb 2012 08:52:39 -0500 Date: Wed, 22 Feb 2012 08:52:39 -0500 From: Ron Peterson To: pgsql-sql@postgresql.org Subject: Re: on insert rule with default value Message-ID: <20120222135239.GI29194@mtholyoke.edu> Mail-Followup-To: pgsql-sql@postgresql.org References: <20120221205130.GG29194@mtholyoke.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20120221205130.GG29194@mtholyoke.edu> Organization: Mount Holyoke College X-Operating-System: Debian GNU/Linux User-Agent: Mutt/1.5.20 (2009-06-14) X-Spam-Score: -2.6 () RP_MATCHES_RCVD X-Scanned-By: MIMEDefang 2.67 on 138.110.1.180 X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/72 X-Sequence-Number: 36365 2012-02-21_15:51:30-0500 Ron Peterson : > My rule below does not insert the the same uuid value into the test_log > table as is created in the test table when I insert a new value. I know > I've worked through this before, but I'm not remembering why this is. > What's a right way to do this? Obviously I can use a trigger function. I'm mostly wondering if there are any tricks to accomplishing this with rules (I like the simple syntax). I suspect the problem here is that 'new' on insert refers to the function used to calculate the new value, /not/ the new value that is actually inserted into the table. There are probably reasons for that; but it would seem nicer to refer to the actual new table value rather than the expression used to calculate it. My 2c. -Ron- > create table test ( > anid > uuid > not null > default encode( gen_random_bytes( 16 ), 'hex' )::uuid > primary key, > value > text > ); > > create table test_log ( > anid > uuid, > value > text, > op > text, > attime > timestamp with time zone > ); > > create rule test_rule_a as > on insert to test do ( > insert into test_log ( anid, value, op, attime ) > values ( new.anid, new.value, 'insert', now() ) > );