Documentation/ Decision records
ADR 0007: Modules refer to each other's rows by id, never by foreign key
Status: Accepted
Context
Every module's tables share one schema (ADR 0004), so the database would accept a foreign key from any table to any other. A foreign key between two modules' tables ties their schemas together: neither module could change, rebuild or drop its own table without the other, deletes would cascade or fail across the boundary, and extracting either module would become a data migration.
Decision
- A column that refers to another module's row is a plain
BIGINTid with no foreign key. Foreign keys exist only between tables of the same module.goods_receipt_linesreferences its owngoods_receiptswith a foreign key, and stock units, unit loads and locations by bare id (V409__create_goods_receipt_lines.sql,goods_receipt_lines);unit_loadsreferences its ownunit_load_typeswith a foreign key and holdsstorage_location_idbare (V102__create_unit_loads.sql,unit_loads). - Migrations say so where they add such a column, for example
V418__add_gr_line_serial_and_packaging.sql,goods_receipt_lines,V425__gr_line_storage_strategy.sql,goods_receipt_lines,V427__delivery_order_destination_operator_sender.sql,karyo.delivery_orders,V609__pick_order_destination.sql,pick_ordersandV1500__create_stream_batches.sql,stream_batches. A scan of every migration finds no foreign key from one module's table to another's. - The owning module answers questions about its rows through a lookup SPI in its
-api:ProductLookup(ProductLookup),StockUnitLookup(StockUnitLookup) and the other lookup interfaces (ADR 0006). - A module that must not remove rows other modules still use asks them through an SPI it declares.
Before stock units and unit loads are purged,
PurgeBlockerLookupis answered by fulfillment, orders, stocktaking and tasks for their own live references (PurgeBlockerLookup).
Consequences
- A module's schema changes stay inside the module, and the id-only references are the seams an extraction would need.
- References to rows that are removed on purpose become tombstones by design: journal rows,
finished picks, receipt lines and closed count lines keep the ids of rows that no longer exist
(
PurgeBlockerLookup). - The database does not protect integrity across modules; the application has to, and does not
everywhere. Two guards are now in place.
LocationService.deleterefuses with 409has-dependentswhile a unit load, a fix assignment, a putaway reservation or an open transport order names the location, asking inventory throughUnitLoadLookup.existsAtLocationand tasks throughTransportDemandLookup.hasOpenOrderAtLocation.UnitLoadTypeService.deleterefuses the same way while a unit load, a type capacity constraint or a product's default names the type, asking layout throughTypeCapacityConstraintLookupand product throughProductLookup.existsWithDefaultUnitLoadType. On the write side, a product's default unit-load type, default storage strategy and zone are checked before they are stored: the owning module implementsProductUnitLoadTypeLookupandProductPlacementLookup, which product declares, and a missing id is refused with 422invalid-configuration. A type capacity constraint and an ASN UL advice ask the sameProductUnitLoadTypeLookupbefore they store a unit-load type id, and a missing id is refused with 422 (invalid-referenceandinvalid-unit-load-type).
Alternatives considered
- Foreign keys across module tables. Rejected: they would bind the modules' schemas to each other and turn any later extraction into a data migration. Keeping references as ids is what keeps a module extractable.
Evidence
services/karyo-app/src/main/resources/db/migration/orders/V409__create_goods_receipt_lines.sql,goods_receipt_lines- a same-module foreign key beside cross-module idsservices/karyo-app/src/main/resources/db/migration/inventory/V102__create_unit_loads.sql,unit_loads- the same split onunit_loadsservices/product-service/karyo-product-api/src/main/kotlin/com/karyo/product/spi/ProductLookup.kt,ProductLookup,services/inventory-service/karyo-inventory-api/src/main/kotlin/com/karyo/inventory/api/spi/StockUnitLookup.kt,StockUnitLookup- lookup SPIsservices/inventory-service/karyo-inventory-api/src/main/kotlin/com/karyo/inventory/api/spi/PurgeBlockerLookup.kt,PurgeBlockerLookup- asking other modules before a deleteservices/warehouse-layout-service/karyo-layout-core/src/main/kotlin/com/karyo/layout/service/LocationService.kt,LocationService.requireNoDependents- the location delete asking inventory and tasks before it deletes- Data and persistence, Warehouse layout configuration