Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: duplicate purchase order lines

Brad Barbin2dd45116981a
diff --git a/packages/database/supabase/migrations/20250417163855_purchase-order-distinct-lines.sql b/packages/database/supabase/migrations/20250417163855_purchase-order-distinct-lines.sql
new file mode 100644
index 0000000000..3a92716b78
--- /dev/null
+++ b/packages/database/supabase/migrations/20250417163855_purchase-order-distinct-lines.sql
@@ -0,0 +1,26 @@
+DROP VIEW IF EXISTS "purchaseOrderLines";
+CREATE OR REPLACE VIEW "purchaseOrderLines" WITH(SECURITY_INVOKER=true) AS (
+ SELECT DISTINCT ON (pl.id)
+ pl.*,
+ CASE
+ WHEN i."thumbnailPath" IS NULL AND mu."thumbnailPath" IS NOT NULL THEN mu."thumbnailPath"
+ WHEN i."thumbnailPath" IS NULL AND imu."thumbnailPath" IS NOT NULL THEN imu."thumbnailPath"
+ ELSE i."thumbnailPath"
+ END as "thumbnailPath",
+ i.name as "itemName",
+ i.description as "itemDescription",
+ COALESCE(mu.id, imu.id) as "modelId",
+ COALESCE(mu."autodeskUrn", imu."autodeskUrn") as "autodeskUrn",
+ COALESCE(mu."modelPath", imu."modelPath") as "modelPath",
+ COALESCE(mu."name", imu."name") as "modelName",
+ COALESCE(mu."size", imu."size") as "modelSize",
+ ic."unitCost" as "unitCost",
+ sp."supplierPartId"
+ FROM "purchaseOrderLine" pl
+ INNER JOIN "purchaseOrder" so ON so.id = pl."purchaseOrderId"
+ LEFT JOIN "modelUpload" mu ON pl."modelUploadId" = mu."id"
+ INNER JOIN "item" i ON i.id = pl."itemId"
+ LEFT JOIN "itemCost" ic ON ic."itemId" = i.id
+ LEFT JOIN "modelUpload" imu ON imu.id = i."modelUploadId"
+ LEFT JOIN "supplierPart" sp ON sp."supplierId" = so."supplierId" AND sp."itemId" = i.id
+);
\ No newline at end of file