suppliers
Companies you buy products from.
Inventory systems answer two questions: how much of each product is where, and how did it get that way. This schema keeps a current count and an append-only history of every change.
Companies you buy products from.
Places where stock is kept.
Items you stock, each with a SKU and a reorder threshold.
The current quantity of a product in a warehouse. A junction table with a count on it.
Every increase or decrease, with a reason. This is the audit trail.
products.supplier_id references suppliers.id: one row in suppliers can have many rows in products.stock_levels is a junction table between warehouses and products: many-to-many.stock_movements.product_id references products.id: one row in products can have many rows in stock_movements.stock_movements.warehouse_id references warehouses.id: one row in warehouses can have many rows in stock_movements.Copy it into your database, or open it in Crowsfoot to edit the diagram, add notes, and export again.
CREATE TABLE suppliers (
id bigint PRIMARY KEY,
name varchar(160) NOT NULL,
email varchar(255)
);
CREATE TABLE warehouses (
id bigint PRIMARY KEY,
name varchar(120) NOT NULL,
location varchar(160)
);
CREATE TABLE products (
id bigint PRIMARY KEY,
supplier_id bigint NOT NULL REFERENCES suppliers (id),
sku varchar(40) NOT NULL,
name varchar(160) NOT NULL,
reorder_level int NOT NULL DEFAULT 0
);
CREATE TABLE stock_levels (
warehouse_id bigint NOT NULL REFERENCES warehouses (id),
product_id bigint NOT NULL REFERENCES products (id),
quantity int NOT NULL DEFAULT 0,
PRIMARY KEY (warehouse_id, product_id)
);
CREATE TABLE stock_movements (
id bigint PRIMARY KEY,
product_id bigint NOT NULL REFERENCES products (id),
warehouse_id bigint NOT NULL REFERENCES warehouses (id),
change int NOT NULL,
reason varchar(40) NOT NULL,
moved_at timestamptz NOT NULL
);
New to ER diagrams? Read the crow’s foot notation guide.