|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
![]() |
| Viewing: Dev Shed Forums > Databases > PostgreSQL Help > Rules problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|