From a80e048c2d054cb24fdc4a3c263eb75aace9c8a2 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Thu, 30 Jun 2022 13:38:30 +0200 Subject: [PATCH] Optimized `product_barcodes` handling (references #1928) --- migrations/0195.sql | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 migrations/0195.sql diff --git a/migrations/0195.sql b/migrations/0195.sql new file mode 100644 index 00000000..9ba8409c --- /dev/null +++ b/migrations/0195.sql @@ -0,0 +1,12 @@ +-- Delete once all barcodes without a valid product_id (those are not visible anywhere) +DELETE FROM product_barcodes +WHERE product_id NOT IN (SELECT id FROM products); + +CREATE TRIGGER prevent_adding_barcodes_for_not_existing_products AFTER INSERT ON product_barcodes +BEGIN + SELECT CASE WHEN(( + SELECT 1 + FROM products p + WHERE id = NEW.product_id + ) ISNULL) THEN RAISE(ABORT, "product_id doesn't reference a existing product") END; +END;