Voorraadbeheer (DBML)

Een voorraadschema is een goed DBML-voorbeeld, omdat het interessante deel de mutatietabel is, waar de meeste regels in zitten.


#Wat dit voorbeeld laat zien

  • Een mutatietabel met een hoeveelheid met teken in plaats van aparte tabellen voor inkomend en uitgaand.

  • Samengestelde unieke beperkingen over twee kolommen.

  • Een zelfverwijzende hiërarchie voor locaties.


#Een uitgewerkt voorbeeld

Open het, bewerk het of kopieer de onderstaande bron.

Voorraadbeheer (DBML)

De bron, die je kunt plakken in een nieuw diagram en bewerken:

Table locations {
  id uuid [pk]
  code varchar(32) [unique, not null]
  name varchar(255)
  parent_id uuid [ref: > locations.id, note: 'Null for a top-level site']
  kind varchar(32) [note: 'site, zone, aisle, bin']
}

Table items {
  id uuid [pk]
  sku varchar(64) [unique, not null]
  name varchar(255) [not null]
  unit varchar(16) [default: 'each']
  reorder_level int [default: 0]
}

Table stock_levels {
  id uuid [pk]
  item_id uuid [ref: > items.id, not null]
  location_id uuid [ref: > locations.id, not null]
  quantity int [not null, default: 0]

  Indexes {
    (item_id, location_id) [unique]
  }
  Note: 'Derived from movements; kept for fast reads'
}

Table movements {
  id uuid [pk]
  item_id uuid [ref: > items.id, not null]
  from_location_id uuid [ref: > locations.id]
  to_location_id uuid [ref: > locations.id]
  quantity int [not null, note: 'Always positive; direction comes from the locations']
  reason varchar(64) [note: 'receipt, transfer, pick, adjustment, writeoff']
  occurred_at timestamptz [not null, default: `now()`]

  Indexes {
    (item_id, occurred_at)
  }
}

#Maak het helemaal van jezelf

  • Een zelfverwijzing (parent_id naar dezelfde tabel) is de manier om in DBML een hiërarchie te tekenen.

  • Kolomnotities zijn de goedkoopste documentatie die je ooit zult schrijven.

  • Markeer afgeleide tabellen als afgeleid, anders schrijft iemand er rechtstreeks naartoe.



Dicht genoeg bij het schema om correct te blijven.