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
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
-
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"Tamas Kosa– Tamas Kosa2014年12月04日 08:10:12 +00:00Commented 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...Tamas Kosa– Tamas Kosa2014年12月04日 10:02:12 +00:00Commented 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.Zia– Zia2014年12月04日 10:13:23 +00:00Commented Dec 4, 2014 at 10:13
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
-
I got an error on this. I don't know where is the mistakeTamas Kosa– Tamas Kosa2014年12月04日 08:08:04 +00:00Commented 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...Tamas Kosa– Tamas Kosa2014年12月04日 10:03:14 +00:00Commented Dec 4, 2014 at 10:03
-
I thought you were asking for postgresql solution (according to tags of this question).Michal Zimmermann– Michal Zimmermann2014年12月04日 13:54:58 +00:00Commented Dec 4, 2014 at 13:54
lang-sql