-- Remove the old score from that costumer in that product
DROP TRIGGER IF EXISTS new_score_before ON stores;
DROP FUNCTION IF EXISTS new_score_before();
CREATE OR REPLACE FUNCTION new_score_before() RETURNS trigger as $$
BEGIN
DELETE FROM products_scores
WHERE NEW.user_id = products_scores.user_id AND
NEW.product_id = products_scores.product_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER new_score_before BEFORE INSERT ON products_scores
FOR EACH ROW EXECUTE PROCEDURE new_score_before();