PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 6th, 2003, 12:41 PM
Paola Paola is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 1 Paola User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Rules problem

Hello I’m Paola and I have an urgent problem. First I did a trigger to delete on cascade but didn’t work, then I did a rule for delete on cascade but they sometimes delete and sometimes don’t.

This is the trigger:

ELSIF TG_OP = ''DELETE'' THEN
old.action := ''DELETE'';
-- first, deletes on items table
-- DELETE FROM items WHERE item_id = old.item_id;

-- second, insert on log table
SELECT INTO version_val version FROM customers_log WHERE customer_id=old.customer_id AND item_id=old.item_id ORDER BY version DESC LIMIT 1;
IF FOUND THEN
version_val := version_val + 1;
INSERT INTO customers_log(item_id, customer_id, customer, action, action_date, author, version) VALUES (old.item_id, old.customer_id, old.customer, old.action, old.action_date, old.author, version_val);
RETURN old;
ELSE
-- RAISE EXCEPTION ''the value cannot have only an DELETE value. Security error (first: INSERT, after: UPDATE OR DELETE) ON customers DELETE TRIGGER'';
RETURN old;
END IF;
END IF;


This is the whole function:

CREATE FUNCTION customers_check_trg() RETURNS OPAQUE AS '
DECLARE
version_val integer :=0;
BEGIN
IF TG_OP = ''INSERT'' THEN
new.action := ''INSERT'';
SELECT INTO version_val version FROM customers_log WHERE customer_id=new.customer_id AND item_id=new.item_id ORDER BY version DESC LIMIT 1;
IF FOUND THEN
version_val := version_val + 1;
INSERT INTO customers_log(item_id, customer_id, customer, action, action_date, author, version) VALUES (new.item_id, new.customer_id, new.customer, new.action, new.action_date, new.author, version_val);
new.version := version_val;
RETURN new;
ELSE
INSERT INTO customers_log(item_id, customer_id, customer, action, action_date, author, version) VALUES (new.item_id, new.customer_id, new.customer, new.action, new.action_date, new.author, 1);
new.version := 1;
RETURN new;
END IF;
ELSIF TG_OP = ''UPDATE'' THEN
new.action := ''UPDATE'';
SELECT INTO version_val version FROM customers_log WHERE customer_id=new.customer_id AND item_id=new.item_id ORDER BY version DESC LIMIT 1;
IF FOUND THEN
version_val := version_val + 1;
INSERT INTO customers_log(item_id, customer_id, customer, action, action_date, author, version) VALUES (new.item_id, new.customer_id, new.customer, new.action, new.action_date, new.author, version_val);
new.version := version_val;
RETURN new;
ELSE
-- RAISE EXCEPTION ''the value cannot have only an UPDATE value. Security error (first: INSERT, after: UPDATE OR DELETE)ON customers UPDATE TRIGGER'';
RETURN new;
END IF;
ELSIF TG_OP = ''DELETE'' THEN
old.action := ''DELETE'';
-- first, deletes on items table
-- DELETE FROM items WHERE item_id = old.item_id;

-- second, insert on log table
SELECT INTO version_val version FROM customers_log WHERE customer_id=old.customer_id AND item_id=old.item_id ORDER BY version DESC LIMIT 1;
IF FOUND THEN
version_val := version_val + 1;
INSERT INTO customers_log(item_id, customer_id, customer, action, action_date, author, version) VALUES (old.item_id, old.customer_id, old.customer, old.action, old.action_date, old.author, version_val);
RETURN old;
ELSE
-- RAISE EXCEPTION ''the value cannot have only an DELETE value. Security error (first: INSERT, after: UPDATE OR DELETE) ON customers DELETE TRIGGER'';
RETURN old;
END IF;
END IF;
END;
' LANGUAGE 'plpgsql';

CREATE TRIGGER customers_trg BEFORE INSERT OR UPDATE OR DELETE ON customers
FOR EACH ROW EXECUTE PROCEDURE customers_check_trg();

This is the rule:

CREATE RULE customers_rule AS
ON DELETE TO customers DO DELETE FROM items WHERE item_id = old.item_id;

and sometimes appears this error:

ERROR: record "old" has no field named "item_id"

The create table statements are:

CREATE SEQUENCE customers_customer_id_seq
START 1
INCREMENT 1
MAXVALUE 9223372036854775807
MINVALUE 1
CACHE 1;

CREATE TABLE customers (
item_id integer NOT NULL,
customer_id bigint DEFAULT nextval('"customers_customer_id_seq"'::text) NOT NULL,
customer character varying(255) NOT NULL,
"action" character varying(50) NOT NULL,
action_date timestamp with time zone DEFAULT now(),
author character varying(24) NOT NULL,
"version" integer NOT NULL
);

CREATE SEQUENCE customers_log_id_seq
START 1
INCREMENT 1
MAXVALUE 9223372036854775807
MINVALUE 1
CACHE 1;


CREATE TABLE customers_log (
id integer DEFAULT nextval('"customers_log_id_seq"'::text) NOT NULL,
item_id integer NOT NULL,
customer_id bigint NOT NULL,
customer character varying(255) NOT NULL,
"action" character varying(50) NOT NULL,
action_date timestamp with time zone DEFAULT now(),
author character varying(24) NOT NULL,
"version" integer NOT NULL
);

I use the log table to record every change in every record.

What can I do?

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Rules problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT