2010-09-04 41 views
2

表上的INSERT會觸發發生以下錯誤的存儲過程。postgresql 8.4 trigger/storedproc錯誤:如何解決它?

ERROR: column "targetedfamily" is of type boolean but expression is of type character varying 
    Hint: You will need to rewrite or cast the expression. 
    Where: PL/pgSQL function "fn_family_audit" line 19 at SQL statement 

而這裏的過錯的存儲過程(請注意,我試圖做CAST解決這個問題(NEW.targetedfamily布爾)似乎不工作)

CREATE OR REPLACE FUNCTION fn_family_audit() RETURNS TRIGGER AS $tr_family_audit$ 
    BEGIN 
     -- 
     -- Create a row in family_audit to reflect the operation performed on family, 
     -- make use of the special variable TG_OP to work out the operation. 
     -- 
     IF (TG_OP = 'DELETE') THEN 
      INSERT INTO public.family_audit values (
     DEFAULT, 'D', OLD.family_id, OLD.familyserialno, OLD.node_id, OLD.sourcetype, OLD.familyname, 
     OLD.familynamelocallang, OLD.hofname, OLD.hofnamelocallang, OLD.targetedfamily, OLD.homeless, 
     OLD.landless, OLD.dependentonlabour, OLD.womenprimaryearner, OLD.landlinenumber, OLD.username , now()); 
      RETURN OLD; 
     ELSIF (TG_OP = 'UPDATE') THEN 
      INSERT INTO public.family_audit values(
     DEFAULT, 'U',NEW.family_id, NEW.familyserialno, NEW.node_id, NEW.sourcetype, NEW.familyname, 
     NEW.familynamelocallang, NEW.hofname, NEW.hofnamelocallang, NEW.targetedfamily, NEW.homeless, 
     NEW.landless, NEW.dependentonlabour, NEW.womenprimaryearner, NEW.landlinenumber, NEW.username , now()); 
      RETURN NEW; 
     ELSIF (TG_OP = 'INSERT') THEN 
      INSERT INTO public.family_audit values(
     DEFAULT, 'I',NEW.family_id, NEW.familyserialno, NEW.node_id, NEW.sourcetype, NEW.familyname, 
     NEW.familynamelocallang, NEW.hofname, NEW.hofnamelocallang, CAST(NEW.targetedfamily AS BOOLEAN), NEW.homeless, 
     NEW.landless, NEW.dependentonlabour, NEW.womenprimaryearner, NEW.landlinenumber, NEW.username , now()); 
      RETURN NEW; 
     END IF; 
     RETURN NULL; -- result is ignored since this is an AFTER trigger 
    END; 
$tr_family_audit$ LANGUAGE plpgsql; 

這裏的表定義

nucleus4=# \d family; 
              Table "public.family" 
     Column  |   Type    |       Modifiers 
---------------------+-----------------------------+------------------------------------------------------------ 
family_id   | integer      | not null default nextval('family_family_id_seq'::regclass) 
familyserialno  | integer      | not null 
sourcetype   | character varying(20)  | not null 
familyname   | character varying(100)  | 
familynamelocallang | character varying(255)  | 
hofname    | character varying(100)  | not null 
hofnamelocallang | character varying(255)  | not null 
targetedfamily  | boolean      | 
homeless   | boolean      | 
landless   | boolean      | 
dependentonlabour | boolean      | 
womenprimaryearner | boolean      | 
landlinenumber  | character varying(20)  | 
username   | character varying(20)  | not null 
adddate    | timestamp without time zone | not null default now() 
updatedate   | timestamp without time zone | not null default now() 
node_id    | integer      | not null 
Indexes: 
    "PK_family" PRIMARY KEY, btree (family_id) 
    "family_idx" UNIQUE, btree (familyserialno, node_id) 
Foreign-key constraints: 
    "family_fk" FOREIGN KEY (node_id) REFERENCES hierarchynode_master(node_id) 
Referenced by: 
    TABLE "agriland" CONSTRAINT "FK_agriland_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT 
    TABLE "currentloans" CONSTRAINT "FK_currentloans_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT 
    TABLE "family_address" CONSTRAINT "FK_family_address_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT 
    TABLE "family_basic_info" CONSTRAINT "FK_family_basic_info_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT 
    TABLE "family_entitlement" CONSTRAINT "FK_family_entitlement_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT 
    TABLE "livestock" CONSTRAINT "FK_livestock_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT 
    TABLE "member" CONSTRAINT "FK_member_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT 
    TABLE "otherassets" CONSTRAINT "FK_otherassets_family" FOREIGN KEY (family_id) REFERENCES family(family_id) ON UPDATE RESTRICT ON DELETE RESTRICT 
Triggers: 
    tr_family_audit AFTER INSERT OR DELETE OR UPDATE ON family FOR EACH ROW EXECUTE PROCEDURE fn_family_audit() 
    tr_family_updatedate BEFORE UPDATE ON family FOR EACH ROW EXECUTE PROCEDURE fn_modify_updatedate_column() 


nucleus4=# 

這裏的family_audit

nucleus4=# \d family_audit; 
               Table "public.family_audit" 
     Column  |   Type    |        Mod 
---------------------+-----------------------------+---------------------------------- 
familyaudit_id  | integer      | not null default nextval('family_ 
operation   | character(1)    | not null 
family_id   | integer      | not null 
familyserialno  | integer      | not null 
sourcetype   | character varying(20)  | not null 
familyname   | character varying(100)  | 
familynamelocallang | character varying(255)  | 
hofname    | character varying(100)  | not null 
hofnamelocallang | character varying(255)  | not null 
targetedfamily  | boolean      | 
homeless   | boolean      | 
landless   | boolean      | 
dependentonlabour | boolean      | 
womenprimaryearner | boolean      | 
landlinenumber  | character varying(20)  | 
username   | character varying(20)  | not null 
adddate    | timestamp without time zone | not null default now() 
node_id    | integer      | not null 
Indexes: 
    "PK_family_audit" PRIMARY KEY, btree (familyaudit_id) 


nucleus4=# 

這裏的觸發

CREATE TRIGGER tr_family_audit 
AFTER INSERT OR UPDATE OR DELETE ON public.family 
    FOR EACH ROW EXECUTE PROCEDURE fn_family_audit(); 

我將不勝感激任何提示。

謝謝

BR,
〜一個

回答

0

你的問題是在這裏:

NEW.hofnamelocallang 

你的刀片具有一個額外的列(顯然NEW.node_id)。試着改變你的插入到:

INSERT INTO public.family_audit values(
     DEFAULT, 'I',NEW.family_id, NEW.familyserialno, 
     NEW.sourcetype, NEW.familyname, 
     NEW.familynamelocallang, NEW.hofname, NEW.hofnamelocallang, 
     NEW.targetedfamily, NEW.homeless, 
     NEW.landless, NEW.dependentonlabour, NEW.womenprimaryearner, 
     NEW.landlinenumber, NEW.username , now() 
); 

你得到基本上是說,你是想,因爲你在插入物把額外的列中插入NEW.hofnamelocallangtargetedfamily列(這是布爾值,而不是爲varchar)錯誤句子。

我會建議,當你正在執行一個插入,出於理智的原因,總是列舉你要放入值的列。類似這樣的:

insert into table foo 
(col1, col2, col3) -- column enumeration here 
values 
(1, 2, 3); 
+0

謝謝。桌子的結構發生了變化。 – anjanb 2010-09-04 16:30:27