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(purchasing,sales): use table-name embeds for composite-FK relationships

Sidwebworksb9fd5a784060
diff --git a/.ai/lessons.md b/.ai/lessons.md
index 396b2b46ee..3169318549 100644
--- a/.ai/lessons.md
+++ b/.ai/lessons.md
@@ -125,3 +125,13 @@ Format: `Context → Problem → Rule → Applies to`
**Rule:** In RR v7 middleware (`callRouteMiddleware`, react-router dist), `next()` wraps the downstream chain in try/catch and **resolves** with `errorHandler(error)`'s Response — it only rejects if `request.signal.aborted`. So mutating headers on the resolved response after `await next()` correctly covers redirects and error (500) responses; only aborted requests skip it, which is fine (client is gone). Register the middleware first so downstream runs inside its `withContext`/ALS scope.
**Applies to:** any RR v7 `middleware`/`clientMiddleware` that reads or mutates the response after `next()`; `packages/logger/src/middleware.server.ts`, `packages/auth/src/middleware/*`.
+
+## Composite (`id, companyId`) FKs break PostgREST `alias:column(...)` embeds
+
+**Context:** RFQ supplier linking silently failed — `getPurchasingRFQSuppliersWithLinks` / `getPurchasingRFQSuppliers` (`purchasing.service.ts`) returned an empty `suppliers` array even though the `purchasingRfqSupplier` row existed, so the Properties multiselect never showed linked suppliers and an optimistic add reverted on revalidation.
+
+**Problem:** The embed `.select("*, supplier:supplierId(id, name)")` uses the `alias:foreignKeyColumn(...)` disambiguation form. That only resolves when `supplierId` is a **single-column** FK. Multi-tenant FKs here are **composite** — `purchasingRfqSupplier_supplierId_fkey FOREIGN KEY ("supplierId","companyId") REFERENCES supplier(id,"companyId")` — so PostgREST returns `PGRST200: Could not find a relationship ... 'supplierId' ... Perhaps you meant 'supplier'`. The whole query errors, `data` is null. Loaders that do `result.data?.map(...) ?? []` (and never check `result.error`) swallow it as "no rows". Same bug hit the nested `supplier:supplierId (*)` inside `supplierQuote:supplierQuoteId(*, ...)` for linked-quote reads.
+
+**Rule:** For a composite-FK relationship, embed by **target table name** — `.select("*, supplier(id, name)")` (or the explicit constraint `supplier:supplier!purchasingRfqSupplier_supplierId_fkey(...)`), never `alias:fkColumn(...)`. Verify a PostgREST embed against the running REST API (`/rest/v1/<table>?select=...` with the service-role key) — PGRST200 is a schema-cache error returned even on empty tables. And when a loader powers UI state, check `.error`, don't `?? []` a failed query into silent emptiness.
+
+**Applies to:** any supabase-js embed on a join table with a composite `(entityId, companyId)` FK — `purchasingRfqSupplier`, `supplierQuote.supplierId`, and siblings; `apps/erp/app/modules/purchasing/purchasing.service.ts`.
diff --git a/apps/erp/app/modules/purchasing/purchasing.service.ts b/apps/erp/app/modules/purchasing/purchasing.service.ts
index 464e2ac494..4eb5a833e8 100644
--- a/apps/erp/app/modules/purchasing/purchasing.service.ts
+++ b/apps/erp/app/modules/purchasing/purchasing.service.ts
@@ -2364,7 +2364,7 @@ export async function getPurchasingRFQSuppliers(
) {
return client
.from("purchasingRfqSupplier")
- .select("*, supplier:supplierId(id, name)")
+ .select("*, supplier(id, name)")
.eq("purchasingRfqId", purchasingRfqId);
}
@@ -2616,7 +2616,7 @@ export async function getLinkedSupplierQuotes(
.select(
`
supplierQuoteId,
- supplierQuote:supplierQuoteId (*, supplier:supplierId (*))
+ supplierQuote:supplierQuoteId (*, supplier(*))
`
)
.eq("purchasingRfqId", purchasingRfqId);
@@ -2688,7 +2688,7 @@ export async function getSiblingQuotesForQuote(
.select(
`
supplierQuoteId,
- supplierQuote:supplierQuoteId (*, supplier:supplierId (*))
+ supplierQuote:supplierQuoteId (*, supplier(*))
`
)
.in("purchasingRfqId", rfqIds)
@@ -2721,7 +2721,7 @@ export async function getSupplierQuotesForComparison(
.select(
`
supplierQuoteId,
- supplierQuote:supplierQuoteId (*, supplier:supplierId (*))
+ supplierQuote:supplierQuoteId (*, supplier(*))
`
)
.eq("purchasingRfqId", purchasingRfqId);
@@ -2780,7 +2780,7 @@ export async function getPurchasingRFQSuppliersWithLinks(
) {
return client
.from("purchasingRfqSupplier")
- .select("*, supplier:supplierId(id, name)")
+ .select("*, supplier(id, name)")
.eq("purchasingRfqId", purchasingRfqId);
}
diff --git a/apps/erp/app/modules/sales/sales.service.ts b/apps/erp/app/modules/sales/sales.service.ts
index b06bbc41a0..eef7444fb4 100644
--- a/apps/erp/app/modules/sales/sales.service.ts
+++ b/apps/erp/app/modules/sales/sales.service.ts
@@ -2827,7 +2827,7 @@ export async function getCustomerItemPriceOverrideById(
.select(
`
*,
- customer:customerId(id, name),
+ customer(id, name),
customerType:customerTypeId(id, name),
item:itemId(id, name),
breaks:customerItemPriceOverrideBreak(id, quantity, overridePrice, active)
@@ -2853,7 +2853,7 @@ export async function getCustomerItemPriceOverridesList(
.select(
`
*,
- customer:customerId(id, name),
+ customer(id, name),
customerType:customerTypeId(id, name),
item:itemId(id, name, unitSalePrice:itemUnitSalePrice(unitSalePrice))
`,
diff --git a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
index 196397b3ef..787dd48072 100644
--- a/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
+++ b/apps/erp/app/routes/api+/mcp+/lib/tool-metadata.json
@@ -1,5 +1,5 @@
{
- "generated": "2026-07-09T01:32:43.903Z",
+ "generated": "2026-07-09T20:31:33.257Z",
"totalTools": 1263,
"modules": 15,
"tools": [