From 8c18a9eb1dbb36fc175695581fa1c5d72c0db3f2 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 4 Nov 2019 19:00:16 +0100 Subject: [PATCH] Ensure that the location_id is never NULL in the stock and stock_log table (checked by an INSERT trigger, sets the products default location if empty) --- migrations/0095.sql | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/migrations/0095.sql b/migrations/0095.sql index dade801a..86486e66 100644 --- a/migrations/0095.sql +++ b/migrations/0095.sql @@ -1,3 +1,19 @@ +CREATE TRIGGER set_products_default_location_if_empty_stock AFTER INSERT ON stock +BEGIN + UPDATE stock + SET location_id = (SELECT location_id FROM products where id = product_id) + WHERE id = NEW.id + AND location_id IS NULL; +END; + +CREATE TRIGGER set_products_default_location_if_empty_stock_log AFTER INSERT ON stock_log +BEGIN + UPDATE stock_log + SET location_id = (SELECT location_id FROM products where id = product_id) + WHERE id = NEW.id + AND location_id IS NULL; +END; + DROP VIEW stock_current_locations; CREATE VIEW stock_current_locations AS SELECT @@ -8,4 +24,4 @@ SELECT FROM stock s JOIN products p ON s.product_id = p.id JOIN locations l on IFNULL(s.location_id, p.location_id) = l.id -GROUP BY s.product_id, IFNULL(s.location_id, p.location_id) +GROUP BY s.product_id, IFNULL(s.location_id, p.location_id);