Templates/Commerce

Inventory & warehouses database schema

Stock levels per warehouse, stock movements, suppliers and purchase orders.

7 tables, 8 relationships

Opens as a new diagram you can change. Free.

Tables

warehouses

  • iduuid not null
  • nametext not null
  • codetext not null
  • addresstext
  • created_attimestamptz not null

suppliers

  • iduuid not null
  • nametext not null
  • emailtext
  • created_attimestamptz not null

products

  • iduuid not null
  • skutext not null
  • nametext not null
  • reorder_levelinteger not null
  • created_attimestamptz not null

stock_levels

  • iduuid not null
  • warehouse_id> warehousesuuid not null
  • product_id> productsuuid not null
  • on_handinteger not null
  • reservedinteger not null
  • created_attimestamptz not null

stock_movements

  • iduuid not null
  • warehouse_id> warehousesuuid not null
  • product_id> productsuuid not null
  • quantityinteger not null
  • reasontext not null
  • referencetext
  • created_attimestamptz not null

purchase_orders

  • iduuid not null
  • supplier_id> suppliersuuid not null
  • warehouse_id> warehousesuuid not null
  • statustext not null
  • expected_atdate
  • created_attimestamptz not null

purchase_order_lines

  • iduuid not null
  • purchase_order_id> purchase_ordersuuid not null
  • product_id> productsuuid not null
  • quantityinteger not null
  • unit_costnumeric(10,2) not null
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TABLE "products" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "sku" TEXT NOT NULL UNIQUE,
  "name" TEXT NOT NULL,
  "reorder_level" INTEGER DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "purchase_order_lines" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "purchase_order_id" UUID NOT NULL,
  "product_id" UUID NOT NULL,
  "quantity" INTEGER NOT NULL,
  "unit_cost" NUMERIC(10,2) NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "purchase_orders" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "supplier_id" UUID NOT NULL,
  "warehouse_id" UUID NOT NULL,
  "status" TEXT DEFAULT 'draft' NOT NULL,
  "expected_at" DATE,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "stock_levels" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "warehouse_id" UUID NOT NULL,
  "product_id" UUID NOT NULL,
  "on_hand" INTEGER DEFAULT 0 NOT NULL,
  "reserved" INTEGER DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "stock_movements" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "warehouse_id" UUID NOT NULL,
  "product_id" UUID NOT NULL,
  "quantity" INTEGER NOT NULL,
  "reason" TEXT NOT NULL,
  "reference" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "suppliers" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "email" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "warehouses" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "code" TEXT NOT NULL UNIQUE,
  "address" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "stock_levels" ADD CONSTRAINT "fk_stock_levels_warehouse_id" FOREIGN KEY ("warehouse_id") REFERENCES "warehouses" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "stock_levels" ADD CONSTRAINT "fk_stock_levels_product_id" FOREIGN KEY ("product_id") REFERENCES "products" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "stock_movements" ADD CONSTRAINT "fk_stock_movements_warehouse_id" FOREIGN KEY ("warehouse_id") REFERENCES "warehouses" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "stock_movements" ADD CONSTRAINT "fk_stock_movements_product_id" FOREIGN KEY ("product_id") REFERENCES "products" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "purchase_orders" ADD CONSTRAINT "fk_purchase_orders_supplier_id" FOREIGN KEY ("supplier_id") REFERENCES "suppliers" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "purchase_orders" ADD CONSTRAINT "fk_purchase_orders_warehouse_id" FOREIGN KEY ("warehouse_id") REFERENCES "warehouses" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "purchase_order_lines" ADD CONSTRAINT "fk_purchase_order_lines_purchase_order_id" FOREIGN KEY ("purchase_order_id") REFERENCES "purchase_orders" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "purchase_order_lines" ADD CONSTRAINT "fk_purchase_order_lines_product_id" FOREIGN KEY ("product_id") REFERENCES "products" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

More Commerce templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.