Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL Trigger Error : control reached end of trigger procedure without RETURN

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?

like image 981
Manish Gupta Avatar asked Jan 31 '17 07:01

Manish Gupta


People also ask

What happens if trigger fails Postgres?

Similarly, if a ddl_command_end trigger fails with an error, the effects of the DDL statement will be rolled back, just as they would be in any other case where the containing transaction aborts. For a complete list of commands supported by the event trigger mechanism, see Section 40.2.

Does trigger return value?

The return value of a trigger function In row level BEFORE triggers, the return value has the following meaning: if the trigger returns NULL, the triggering operation is aborted, and the row will not be modified. for INSERT and UPDATE triggers, the returned row is the input for the triggering DML statement.

What is trigger Plpgsql?

Triggers on Events. PL/pgSQL can be used to define trigger functions on data changes or database events. A trigger function is created with the CREATE FUNCTION command, declaring it as a function with no arguments and a return type of trigger (for data change triggers) or event_trigger (for database event triggers).

How do you check if trigger is enabled in Postgres?

If tgenabled is 'D', the trigger is disabled. All other values (documented here) indicate, that it is enabled in some way. The cast to the regclass type gets you from qualified table name to OID (object id) the easy way.


1 Answers

Try using this:

   END LOOP;
   RETURN NULL;
like image 86
User0706 Avatar answered Sep 18 '22 19:09

User0706