From fe363fd4dbde010097353700b06e378b7b62e352 Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 19 Jul 2024 15:00:13 +0200 Subject: [PATCH] fix: refs #7126 Primary key no unique data --- .../00-firstScript.sql | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/db/versions/11042-turquoiseAspidistra/00-firstScript.sql b/db/versions/11042-turquoiseAspidistra/00-firstScript.sql index d3a3f0c00..4910a441f 100644 --- a/db/versions/11042-turquoiseAspidistra/00-firstScript.sql +++ b/db/versions/11042-turquoiseAspidistra/00-firstScript.sql @@ -32,4 +32,23 @@ ALTER TABLE bs.waste CHANGE saleWaste saleInternalWaste decimal(10,2) DEFAULT NU ALTER TABLE bs.waste ADD saleExternalWaste decimal(10,2) DEFAULT NULL NULL; ALTER TABLE bs.waste CHANGE saleQuantity saleQuantity decimal(10,2) DEFAULT NULL NULL AFTER itemTypeFk; ALTER TABLE bs.waste CHANGE itemFk itemFk int(11) DEFAULT 0 NOT NULL AFTER itemTypeFk; -ALTER TABLE bs.waste ADD CONSTRAINT waste_pk PRIMARY KEY (`year`, `week`, buyerFk, itemTypeFk, itemFk); \ No newline at end of file + +CREATE OR REPLACE TEMPORARY TABLE tWasteUnique + SELECT * + FROM bs.waste + GROUP BY `year`, `week`, buyerFk, itemTypeFk, itemFk + HAVING COUNT(*) > 1; + +DELETE w.* FROM bs.waste w + JOIN tWasteUnique twu ON twu.`year` = w.`year` + AND twu.`week` = w.`week` + AND twu.`buyerFk` = w.`buyerFk` + AND twu.`itemTypeFk` = w.`itemTypeFk` + AND twu.`itemFk` = w.`itemFk`; + +INSERT INTO bs.waste + SELECT * FROM tWasteUnique; + +ALTER TABLE bs.waste ADD CONSTRAINT waste_pk PRIMARY KEY (`year`, `week`, buyerFk, itemTypeFk, itemFk); + +DROP TEMPORARY TABLE tBuyers, tWasteUnique;