pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ahti Legonkov <lego@127.0.0.1>
To: pgsql-sql@postgresql.org
Subject: rules / triggers on insert. why after?
Date: Fri, 12 Jul 2002 12:24:01 +0300
Message-ID: <3D2EA031.3000104@127.0.0.1> (raw)

Hi,

I have the following things in my database:

CREATE SEQUENCE "REO_ID_seq" INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 
CACHE 1;

CREATE TABLE reo (
   "REO_ID" int4 DEFAULT nextval('"REO_ID_seq"'::text) NOT NULL,
   "TYPE" varchar(64) NOT NULL,
   CONSTRAINT "REO_ID_reo_ukey" UNIQUE ("REO_ID"),
   CONSTRAINT reo_pkey PRIMARY KEY ("REO_ID")
);

CREATE TABLE lreo (
   "CITY" varchar(64),
   "STREET" varchar(64),
   "PRICE" int4,
   "REO_ID" int4 DEFAULT currval('"REO_ID_seq"'::text),
   CONSTRAINT "REO_ID_land_reo_ukey" UNIQUE ("REO_ID"),
   CONSTRAINT "fk_LREO_REO_ID" FOREIGN KEY ("REO_ID") REFERENCES reo 
("REO_ID") ON DELETE NO ACTION ON UPDATE NO ACTION NOT DEFERRABLE 
INITIALLY IMMEDIATE
);

---- either this
-- the rule won't work in 7.2.x, because it is done AFTER the Insert has
-- executed :(
CREATE RULE "rule_lreo_INSERT" AS ON INSERT
     TO lreo
     DO INSTEAD INSERT INTO reo("TYPE") values ('lreo');
---- or this
-- but why does not this work ???
CREATE FUNCTION insert_lreo() Returns opaque As '
Begin
	INSERT INTO reo ("TYPE") VALUES (''lreo'');
	Return Null;
End;
' language 'plpgsql';


CREATE TRIGGER on_insert_lreo
	BEFORE INSERT ON lreo FOR EACH ROW
	EXECUTE PROCEDURE insert_lreo();
---- end

On postgres 7.1.3 it was OK to do this:

INSERT INTO "lreo" ("STREET", "PRICE", "CITY")
VALUES ('street', 1234, 'the city');

but on postgres 7.2 and 7.2.1 I get an error that "REO_ID_seq.currval is 
not yet defined for this session." The trigger should execute *before* 
the insert, right?

Does anyone know why since postgres 7.2 the rules are executed *after* 
the insert?

Hope you can help me:)

-- 
Ahti Legonkov




view thread (3+ messages)  latest in thread

Message-ID: <3D2EA031.3000104@127.0.0.1>
Permalink:  ../3D2EA031.3000104@127.0.0.1/
Also on:    postgresql.org/message-id/3D2EA031.3000104@127.0.0.1

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: lego@127.0.0.1
  Subject: Re: rules / triggers on insert. why after?
  In-Reply-To: <3D2EA031.3000104@127.0.0.1>

* 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