I have an INSTEAD OF INSERT
function on a view that is intended to insert data into different tables, based on the SRID of a geometry data column.
Inside the function, I'm unable to successfully execute an IF statement which then executes the INSERT but outside the function, using the same logic the IF statement works as expected.
I've tested the logic outside of the trigger, this returns NOTICE: yes
:
DO $$
DECLARE geom geometry;
BEGIN
geom := cast('srid=3857;Point (1 1)' as geometry);
IF ST_SRID(geom) = 3857
THEN RAISE NOTICE 'yes';
END IF;
END $$
But inside a function like this:
CREATE OR REPLACE FUNCTION insert_from_view()
RETURNS trigger
LANGUAGE 'plpgsql'
AS $$
BEGIN
--this notice is here to show that inside the function the SRID is being read correctly
RAISE NOTICE 'SRID: %',ST_SRID(NEW.geom);
-- try the if
IF ST_SRID(NEW.geom) = 3857
THEN raise notice 'yes';
ELSE raise notice 'SRID % not found', ST_SRID(NEW.geom); END IF;
RETURN NEW;
END
$$;
Then executing this query:
INSERT INTO view
VALUES (cast('srid=3857;Point (1 1)' as geometry))
Returns: NOTICE: SRID: 3857 NOTICE: SRID 3857 not found
Is there a reason why inside the function this logic does not work?