Delete Trigger

From: Chris Berry <chrisdberry82(at)googlemail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Delete Trigger
Date: 2010-08-14 07:54:56
Message-ID: AANLkTimmJ+1f-5CtXVN8hMb-bhzyngwGhAv2Xr0Zt==_@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

When I update, insert or delete to one table, I need a trigger to delete and
possibly insert into another table. The trigger I have works for insert and
update, but returns an error when I do a delete.

The is no explicit link between the tables, so I can't do 'DELETE CASCADE'
or anything like that.

Here is the trigger:

CREATE OR REPLACE FUNCTION event_tweet() RETURNS TRIGGER AS
$textetweet_textetweet$
DECLARE
new_id varchar;
new_date date;
BEGIN
DELETE FROM textetweet_textetweet ;
IF(TG_OP='INSERT') THEN
DELETE FROM textetweet_textetweet WHERE link='/ievent/' || NEW.id;
INSERT INTO textetweet_textetweet (date,title,content,link,creation) values
(NEW.date,'new event','new event details','/ievent/' || NEW.id, NOW());
END IF;
IF(TG_OP='UPDATE') THEN
DELETE FROM textetweet_textetweet WHERE link='/ievent/' || NEW.id;
INSERT INTO textetweet_textetweet (date,title,content,link,creation) values
(NEW.date,'new event','new event details','/ievent/' || NEW.id, NOW());
END IF;
IF(TG_OP='DELETE') THEN
DELETE FROM textetweet_textetweet WHERE link='/ievent/' || NEW.id;
END IF;

The problem seems to be the 'NEW.id'. How do I get the deleted id for the
trigger?

Thanks a lot

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sandeep Srinivasa 2010-08-14 09:03:20 .psqlrc and custom functions (mysql-like)
Previous Message Scott Marlowe 2010-08-14 07:42:08 Re: Windows 2003 server installation issue