1

I have two spatial tables and I am mirroring the data between the two table with a postgres rule:

 CREATE OR REPLACE RULE create_centroid AS ON INSERT TO csg.client_area_request DO INSERT INTO csg.stats (date_needed, date_requested, notice_period, project_name, project_reference, the_geom_webmercator) VALUES (new.date_needed, new.date_requested, new.notice_period, new.project_name, new.project_reference, st_centroid(new.the_geom_webmercator));

Is there any way to do the same process with trigger?

asked Dec 3, 2014 at 15:45

2 Answers 2

2

Create the procedural function as

CREATE OR REPLACE create_centroid()
RETURNS TRIGGER AS 
$body$
BEGIN
 INSERT INTO csg.stats (date_needed, date_requested, notice_period, project_name, project_reference, the_geom_webmercator) VALUES (NEW.date_needed, NEW.date_requested, NEW.notice_period, NEW.project_name, NEW.project_reference, st_centroid(NEW.the_geom_webmercator));
 RAISE NOTICE 'Inserted';
 RETURN null;
END
$body$ language plpgsql volatile;

and create trigger (as was explained by the @zimmi) as

CREATE TRIGGER create_centroid AFTER INSERT ON csg.client_area_request
 FOR EACH ROW EXECUTE PROCEDURE create_centroid();

I was using this trigger to update the stats table. Should work.

answered Dec 3, 2014 at 20:21
3
  • I got an error on this. I can save it if I change to "create or replace function..." But when I add a new line to my table the trigger generate an error: "infinite recursion detected in rules for relation" Commented Dec 4, 2014 at 8:10
  • I think both triggers are good. I tested my personal postgres db and there they just work fine (with some changes) but somehow they don't work in cartodb... Commented Dec 4, 2014 at 10:02
  • @TamasKosa Sorry, but I have tested for postgres db only. You should instead change the question to mention cartodb. Commented Dec 4, 2014 at 10:13
0
CREATE OR REPLACE FUNCTION create_centroid() RETURNS TRIGGER
$$
 BEGIN
 INSERT INTO csg.stats (date_needed, date_requested, notice_period, project_name, project_reference, the_geom_webmercator) VALUES (NEW.date_needed, NEW.date_requested, NEW.notice_period, NEW.project_name, NEW.project_reference, st_centroid(NEW.the_geom_webmercator));
 END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER create_centroid AFTER INSERT ON csg.client_area_request
 FOR EACH ROW EXECUTE PROCEDURE create_centroid();

should work just fine (not tested though)

answered Dec 3, 2014 at 16:07
3
  • I got an error on this. I don't know where is the mistake Commented Dec 4, 2014 at 8:08
  • I think both triggers are good. I tested my personal postgres db and there they just work fine (with some changes) but somehow they don't work in cartodb... Commented Dec 4, 2014 at 10:03
  • I thought you were asking for postgresql solution (according to tags of this question). Commented Dec 4, 2014 at 13:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.