Karyo
Documentation/ Decision records

ADR 0007: Modules refer to each other's rows by id, never by foreign key

Karyo 2.1.1rendered at build time
On this page

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 BIGINT id with no foreign key. Foreign keys exist only between tables of the same module. goods_receipt_lines references its own goods_receipts with a foreign key, and stock units, unit loads and locations by bare id (V409__create_goods_receipt_lines.sql, goods_receipt_lines); unit_loads references its own unit_load_types with a foreign key and holds storage_location_id bare (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_orders and V1500__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, PurgeBlockerLookup is 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.delete refuses with 409 has-dependents while a unit load, a fix assignment, a putaway reservation or an open transport order names the location, asking inventory through UnitLoadLookup.existsAtLocation and tasks through TransportDemandLookup.hasOpenOrderAtLocation. UnitLoadTypeService.delete refuses the same way while a unit load, a type capacity constraint or a product's default names the type, asking layout through TypeCapacityConstraintLookup and product through ProductLookup.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 implements ProductUnitLoadTypeLookup and ProductPlacementLookup, which product declares, and a missing id is refused with 422 invalid-configuration. A type capacity constraint and an ASN UL advice ask the same ProductUnitLoadTypeLookup before they store a unit-load type id, and a missing id is refused with 422 (invalid-reference and invalid-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 ids
  • services/karyo-app/src/main/resources/db/migration/inventory/V102__create_unit_loads.sql, unit_loads - the same split on unit_loads
  • services/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 SPIs
  • services/inventory-service/karyo-inventory-api/src/main/kotlin/com/karyo/inventory/api/spi/PurgeBlockerLookup.kt, PurgeBlockerLookup - asking other modules before a delete
  • services/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
  • ADR 0004 - the shared schema that makes this a rule rather than a necessity
  • ADR 0006 - the lookup SPIs live in -api modules
  • ADR 0011 - the one place that reads another module's tables directly