I am trying to create a trigger on a column of my table like this in Postgresql 9.5:
CREATE OR REPLACE FUNCTION app.combo_min_stock()
RETURNS TRIGGER AS
$combo_sync$
DECLARE combo_product_ids INTEGER[] := array(SELECT combo_product_map.combo_productid FROM app.combo_product_map WHERE combo_product_map.productid=NEW.productid);
DECLARE comboid INTEGER;
BEGIN
-- UPDATE MINIMUM STOCK FOR COMBO SKUS --
FOREACH comboid IN ARRAY combo_product_ids
LOOP
UPDATE app.inventory SET
good_stock = combo_data.min_good_stock,
bad_stock = combo_data.min_bad_stock,
to_be_updated = true
FROM
(SELECT
product.productid,
MIN(inventory.good_stock) as min_good_stock,
MIN(inventory.bad_stock) as min_bad_stock
FROM
app.product,
app.inventory,
app.combo_product_map
WHERE
product.is_combo=true AND
product.productid=comboid AND
product.productid=combo_product_map.combo_productid AND
combo_product_map.productid=inventory.productid
GROUP BY
product.productid) AS combo_data
WHERE
combo_data.productid=inventory.productid;
END LOOP;
END;
$combo_sync$
LANGUAGE plpgsql;
ALTER FUNCTION app.combo_min_stock()
OWNER TO postgres;
CREATE TRIGGER combo_sync
AFTER UPDATE OF good_stock
ON app.inventory
FOR EACH ROW
EXECUTE PROCEDURE app.combo_min_stock();
When I try to edit a value for good_stock column in my inventory table, it is throwing me this error:
An error has occurred: ERROR: control reached end of trigger procedure without RETURN CONTEXT: PL/pgSQL function app.combo_min_stock()
What is wrong with this query?
Try using this:
END LOOP;
RETURN NULL;