dbTalk Databases Forums  

Trigger is preventing deletion of rows?

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Trigger is preventing deletion of rows? in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Melvin Toy
 
Posts: n/a

Default Trigger is preventing deletion of rows? - 11-05-2006 , 03:30 PM






Hi-

Newbie to postgresql. I download and complied 8.15 on RH 9. I wrote a
trigger that records the actions of customer table into a customer
history table. INSERT and UPDATE work fine but it's prevents any
deletions in the customer table. It records a deletion in the customer
history table but psql says zero rows deleted in the customer table. If
I disable the trigger, a deletion works fine. I tried deleting the
entire if/then/else block in the function and it still prevents deletions.

What am I doing wrong?

thanks,
Melvin

CREATE OR REPLACE FUNCTION customer_trigger() returns trigger as $$

BEGIN
IF (TG_OP = 'INSERT') THEN
NEW.last_update := CURRENT_DATE;
NEW.user_name := USER;
ELSIF (TG_OP = 'UPDATE') THEN
NEW.last_update := CURRENT_DATE;
NEW.user_name := USER;
INSERT INTO customer_history (customer_number, lname, fname,
street, city, state, zip,
credit_limit, balance,
last_update, user_name, action)
VALUES (OLD.customer_number, OLD.lname, OLD.fname,
OLD.street, OLD.city, OLD.state, OLD.zip,
OLD.credit_limit, OLD.balance, CURRENT_DATE, USER,
'UPDATE');
ELSIF (TG_OP = 'DELETE') THEN
INSERT INTO customer_history (customer_number, lname, fname,
street, city, state, zip,
credit_limit, balance,
last_update, user_name, action)
VALUES (OLD.customer_number, OLD.lname, OLD.fname,
OLD.street, OLD.city, OLD.state, OLD.zip,
OLD.credit_limit, OLD.balance, CURRENT_DATE, USER,
'DELETE');
END IF;
return NULL;
END;
$$ language plpgsql;

CREATE TRIGGER tg_all_customer
BEFORE UPDATE OR INSERT OR DELETE ON customer
FOR EACH ROW EXECUTE PROCEDURE customer_trigger();

Reply With Quote
  #2  
Old   
Filip RembiaƂkowski
 
Posts: n/a

Default Re: Trigger is preventing deletion of rows? - 11-07-2006 , 06:04 PM






At 2006-11-05 21:30 Melvin Toy said:
Quote:
Hi-

Newbie to postgresql. I download and complied 8.15 on RH 9. I wrote a
trigger that records the actions of customer table into a customer
history table. INSERT and UPDATE work fine but it's prevents any
deletions in the customer table. It records a deletion in the customer
history table but psql says zero rows deleted in the customer table. If
I disable the trigger, a deletion works fine. I tried deleting the
entire if/then/else block in the function and it still prevents deletions.

What am I doing wrong?

thanks,
Melvin

CREATE OR REPLACE FUNCTION customer_trigger() returns trigger as $$

BEGIN
IF (TG_OP = 'INSERT') THEN
NEW.last_update := CURRENT_DATE;
NEW.user_name := USER;
ELSIF (TG_OP = 'UPDATE') THEN
NEW.last_update := CURRENT_DATE;
NEW.user_name := USER;
INSERT INTO customer_history (customer_number, lname, fname,
street, city, state, zip,
credit_limit, balance,
last_update, user_name, action)
VALUES (OLD.customer_number, OLD.lname, OLD.fname,
OLD.street, OLD.city, OLD.state, OLD.zip,
OLD.credit_limit, OLD.balance, CURRENT_DATE, USER,
'UPDATE');
ELSIF (TG_OP = 'DELETE') THEN
INSERT INTO customer_history (customer_number, lname, fname,
street, city, state, zip,
credit_limit, balance,
last_update, user_name, action)
VALUES (OLD.customer_number, OLD.lname, OLD.fname,
OLD.street, OLD.city, OLD.state, OLD.zip,
OLD.credit_limit, OLD.balance, CURRENT_DATE, USER,
'DELETE');
END IF;
return NULL;
END;
$$ language plpgsql;

CREATE TRIGGER tg_all_customer
BEFORE UPDATE OR INSERT OR DELETE ON customer
FOR EACH ROW EXECUTE PROCEDURE customer_trigger();

Your trigger returns NULL, and is fired BEFORE the actual operation.
That means skipping the operation for the current row.
http://www.postgresql.org/docs/8.1/s...GER-DEFINITION


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.