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.
paths: - "packages/database/supabase/migrations/material.sql" - "apps/erp/app/modules/items/items.service.ts" - "apps/erp/app/modules/items/items.models.ts"
Material Tables Schema
Carbon's material taxonomy: a material row carries the structured properties (form,
substance, dimension, finish, grade, type) for a material item. Spread across several
timestamped migrations — newest wins; the generated types
(packages/database/src/types.ts) are the source of truth for current columns.
Key migrations (in order): 20240608114413_materials.sql (original material/materialForm/
materialSubstance), 20250719100358_material-seed.sql (adds materialFinish, materialGrade,
materialType, materialDimension + code cols + system seed data),
20250721100955 / 20250721101110 (the big shape change: text→FK id columns),
20250519122022_revisions.sql (drops material.itemId), 20251122152701_materials-view-fix.sql
(newest materials view), 20260208020701 (adds ingot form).
material table (current columns)
Composite PK ("id", "companyId"). Columns (per generated types):
id, companyId, materialFormId, materialSubstanceId, dimensionId, finishId,
gradeId, materialTypeId (all the *Id cols nullable, each an FK to its taxonomy table),
approved BOOLEAN, approvedBy, customFields JSONB, tags TEXT[], plus audit
(createdBy/At, updatedBy/At).
- There is no
itemIdcolumn — dropped in20250519122022_revisions.sql(same change hitpart/tool/consumable). Linkage is now positional:material.id == item.readableId(per company). See "Item linkage" below. - There are no
dimensions/finish/gradeTEXT columns — those were dropped in20250721101110_material-drop-text-columns.sqland replaced by thedimensionId/finishId/gradeIdFKs (added20250721100955).materialTypeIdwas added in the same drop migration.
Taxonomy tables
These are global-or-company lookups: companyId is nullable; rows with companyId = NULL
are system-wide (seeded by system) and visible to all companies, company rows override per-company.
RLS lets authenticated users read global rows and employees read their company's rows.
| Table | Key cols | Scoped to | Uniqueness |
|---|---|---|---|
materialForm |
id, name, code, companyId |
— (shapes: sheet, plate, roundbar, ingot…) | (code, companyId) |
materialSubstance |
id, name, code, companyId |
— (steel, aluminum, stainless…) | (code, companyId) |
materialDimension |
id, name, materialFormId, isMetric, companyId |
a form | (materialFormId, name, companyId) |
materialFinish |
id, name, materialSubstanceId, companyId |
a substance | (materialSubstanceId, name, companyId) |
materialGrade |
id, name, materialSubstanceId, companyId |
a substance | (materialSubstanceId, name, companyId) |
materialType |
id, name, code, materialSubstanceId, materialFormId, companyId |
substance + form | (substance,form,code,company) and (substance,form,name,company) |
materialDimension/materialFinish/materialGrade/materialTypehave no audit columns and nocustomFields(onlymaterialForm/materialSubstancedo). Their seed ids are human-readable (e.g.steel-1018,sheet-1-16,mill-ti), notxid().materialTypeis keyed by both a form and a substance (e.g. Hot Rolled steel plate).
Views (all SECURITY_INVOKER=true)
materials(newest:20251122152701_materials-view-fix.sql) — the list/detail view. Joinsmaterial mtoitem ioni."readableId" = m."id" AND i."companyId" = m."companyId", resolves the*IdFKs to display names (materialForm,materialSubstance,dimensions,finish,grade,materialType), aggregatessupplierPartids, and rolls up itemrevisions.materialDimensions→ joinsmaterialForm, exposesformName(+isMetric).materialFinishes/materialGrades→ joinmaterialSubstance, exposesubstanceName.materialTypes→ joins both, exposessubstanceName+formName.
Item linkage (important)
A material is an item of type = 'Material'. material.id is set to the item's readableId,
not the item's id. Creating a material inserts the item row(s) first then the material row
with the shared id — see upsertMaterial in
apps/erp/app/modules/items/items.service.ts (the readable id is material.id, or the id derived
from the properties when companySettings.materialGeneratedIds is on, then
client.from("material").upsert({ id: readableId, ... })). Because the join is on readableId,
one material can have multiple item revisions (and per-size item rows); the materials view
de-dupes to the latest item per (readableId, companyId) and aggregates the revisions.
getMaterial calls the get_material_details(item_id) RPC (defined/revised in the
20250721101110 and 20250725140205_material-details.sql migrations). There is also
get_material_naming_details(readable_id) (builds the readable name/code from the taxonomy).
Editing properties
updateMaterialProperties(client, db, material) is the one path for substance, form, type,
finish, grade and dimension changes: the properties panel (x+/items+/update.tsx, through setMaterialProperties),
upsertMaterial's update branch and MCP all go through it. The rules are pure functions in
apps/erp/app/modules/items/material-properties.ts (pinned by apps/erp/test/material-properties.test.ts);
the service loads the rows they read and writes what they decide.
- A new substance clears finish, grade and type; a new form clears dimension and type; unless the
same call sets them (
resolveMaterialProperties). This mirrors the panel's cascading pickers;MaterialFormclears the same fields on create. - Grade and finish must belong to the substance, dimension to the form, type to both
(
checkMaterialProperties). Only a value the call changes, or whose parent changed, is checked, so an unrelated edit never fails on older data. Refusals areruleErrors, so MCP shows them. - With
materialGeneratedIdson, the readable id and name are derived withgetMaterialId/getMaterialDescriptiononce the material has a substance and a form (generateMaterialIdentity); until then it keeps its id, so the panel can set them one at a time. Clearing a substance or form that is set is refused. Create requires both. - Writes go through Kysely in one transaction (
writeMaterialIdentity): the material row and, on a rename, every revision's item row;upsertMaterialadds the item row, custom fields and itemCost to the same transaction. Kysely bypasses RLS, sorequireMaterialUpdatablefirst runs an UPDATE through the caller's client (material's policy is parts_update in the company, which also grants the item and itemCost rows). A taken readable id is refused on rename and on create. upsertMaterialupdates write only the keys present on the payload (a present-but-undefined key, a cleared form field, clears). The MCP schema requiresname,replenishmentSystem,defaultMethodType,itemTrackingTypeandunitOfMeasureCode, so those are always sent. WithmaterialGeneratedIdson, a sentnameis ignored (materialItemUpdateFields): the name is derived and written to every revision.activeis never touched;postingGroupId/unitCostgo toitemCost;readableIdrenames when ids are hand-typed;sizesadds a revision per new size viacreateRevisionafter the transaction, refused before any write while the material is open in a change notice. The pick method and shelf life are also written after the transaction, so a failure there leaves the transaction's writes in place. An update by readable id is refused when the material has several revisions (sizes are revisions); pass the item id.materialDimension,materialFinish,materialGradeandmaterialTypehave no audit columns, so their service functions initems.server.tsstamp onlycompanyId(payload(ctx, input).build(); thestamp-matches-columnscheck fails a stamped field the table has no column for). Their update branch filters bycompanyIdwhen given and never writes it, so an update cannot move a row to another company.
Code map
- Service:
apps/erp/app/modules/items/items.service.ts—getMaterial,getMaterials(queriesmaterialsview),upsertMaterial, and full CRUD for each taxonomy table (get/upsert/deleteMaterialForm|Substance|Dimension|Finish|Grade|Type, plus*Listhelpers that feed the cascading selects). - Validator:
materialValidatorinapps/erp/app/modules/items/items.models.ts— mergesitemValidatorwith{ id, materialSubstanceId?, materialFormId?, materialTypeId?, finishId?, gradeId?, dimensionId?, sizes? }.
Gotchas
- Newest migration wins. The shape changed twice after creation: text props →
*IdFKs (20250721*), anditemIddropped (20250519122022). Don't trust the seed migration alone. material.id= itemreadableId, not itemid; there is noitemIdFK. Join viareadableId+companyId.- Property/type/dimension lookups are global (
companyId IS NULL) or company-scoped — filter accordingly; don't assume every row is company-owned. 20250809000000_add-material-configurator-type.sqladds'material'to theconfigurationParameterDataTypeenum (configurator can reference a material) — unrelated to the taxonomy tables.