问题
I've spent all afternoon searching on this problem w/o any luck.
I have tried this with both a Rule and a Trigger and get the same behavior. I can create the Rule or Trigger and test it locally by inserting a record manually into the DB using 'psql' yet when I try to insert a record remotely using a JAVA app, the triggers do not fire.
A simplified example is this:
I have a table "Customer_Address" that has fields:
- customerid
- zipcode
- city
- state
I have tried to create both rules and triggers so that when a new record gets inserted into "Customer_Address" with just a zipcode, my trigger/rule will take the zipcode and using a nested subquery it will query the City & State from a lookup table and then set the City & State fields using the return values from the nested subqueries.
I can get this to work with either a trigger or a rule no problems yet only when I insert the record locally on the DB server using 'psql'.
If I attempt to insert the record remotely using the JAVA App, the record inserts fine yet the trigger nor rule never fires.
I even tried the command "alter table Customer_Address enable always trigger update_address_trig";
Yet the trigger never fires from the remote insert. Same goes with the a Rule. Since both the Trigger and Rule work fine locally from the psql command line, and since the rest of the fields get inserted from the JAVA App I am fairly sure that I have my Trigger/Rule syntax correct along with remote accessibility to the DB correctly configured, etc. I am even using the same DB user at both the psql command line and through the JAVA App.
I found various solutions to this problem for MS SQL Server yet none for Postgresql.
Anyone have any ideas? Why should a Trigger behave differently from a local vs remote insert?
Thx in advance!
Brig
The trigger code is:
create or replace function update_event_stage() returns trigger as
$$
begin
update Event_stage set EventCity = (select primarycity from zipmap where zipcode = Event_stage.Event_ZipCode) where EventCity is null;
RETURN NEW;
end
$$ LANGUAGE plpgsql;
create trigger update_event_stage_trig
AFTER INSERT or UPDATE on event_stage
FOR EACH ROW EXECUTE PROCEDURE update_event_stage();
alter table Event_stage enable always trigger update_event_stage_trig;
****** (Added final comment 02/26/2014 20:40 PST)*********
OK thanks again but I had seen the debug docs info earlier and added RAISE clauses yet I got no errors hence why I asked where they should go cuz I thought maybe I was using them in the wrong place.
Anyway still no errors, nothing in Firebug, java App inserts to table fine yet no trigger fire. Manual insert with psql fires trigger and updates City based on zipcode lookup as coded.
I will keep hacking at it, I only posted cuz I saw that on other DBs like Oracle & SQl Server there were specific parameters that affected behavior like this and thought maybe Postgres had the same yet maybe was undocumented but I guess not.
Had I not seen the triggers and rules work fine locally I would have suspected my trigger coding was erroneous yet seeing them work as designed locally through psql made me assume this was some other issue than the Trigger coding.
Thx!
来源:https://stackoverflow.com/questions/22057682/postgresql-triggers-rules-not-firing-for-remote-inserts