From: Ian Meyer <ianmmeyer@gmail.com>
To: pgsql-sql@postgresql.org
Subject: Problem with function and trigger...
Date: Wed, 28 Sep 2005 13:35:09 -0400
Message-ID: <b541050805092810357cac86c0@mail.gmail.com> (raw)
I have a function declared as such:
CREATE OR REPLACE FUNCTION thread_sync() RETURNS trigger AS $$
BEGIN
IF TG_OP = 'DELETE' AND OLD.deleted = FALSE THEN
UPDATE member SET total_threads=total_threads-1 WHERE id=OLD.member_id;
RETURN OLD;
ELSEIF TG_OP = 'INSERT' THEN
UPDATE member SET total_threads=total_threads+1 WHERE id=NEW.member_id;
RETURN NEW;
ELSEIF TG_OP = 'UPDATE' AND NEW.deleted = TRUE THEN
UPDATE member SET total_threads=total_threads-1 WHERE id=NEW.member_id;
RETURN NEW;
ELSEIF TG_OP = 'UPDATE' AND NEW.deleted = FALSE THEN
UPDATE member SET total_threads=total_threads+1 WHERE id=NEW.member_id;
RETURN NEW;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
And the trigger for it:
CREATE TRIGGER thread_sync AFTER INSERT OR DELETE OR UPDATE ON thread
FOR EACH ROW EXECUTE PROCEDURE thread_sync();
creating the function works fine, as well as creating the trigger, but
when I go to insert a row, I get the following message:
bcodev=> insert into thread
(member_id,subject,category_id,last_member_id) values (1,'hi there
this is a test',1,1);
ERROR: record "old" is not assigned yet
DETAIL: The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT: PL/pgSQL function "thread_sync" line 2 at if
What am I failing to understand with this?
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: ianmmeyer@gmail.com
Subject: Re: Problem with function and trigger...
In-Reply-To: <b541050805092810357cac86c0@mail.gmail.com>
* 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