Templates/Commerce

Subscription box database schema

Monthly boxes, what goes in each, subscribers and their shipments.

7 tables, 7 relationships

Opens as a new diagram you can change. Free.

Tables

customers

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

box_plans

  • iduuid not null
  • nametext not null
  • pricenumeric(8,2) not null
  • frequencytext not null
  • created_attimestamptz not null

boxes

  • iduuid not null
  • plan_id> box_plansuuid not null
  • monthdate not null
  • created_attimestamptz not null

products

  • iduuid not null
  • nametext not null
  • costnumeric(8,2) not null
  • created_attimestamptz not null

box_items

  • iduuid not null
  • box_id> boxesuuid not null
  • product_id> productsuuid not null
  • quantityinteger not null
  • created_attimestamptz not null

subscriptions

  • iduuid not null
  • customer_id> customersuuid not null
  • plan_id> box_plansuuid not null
  • activeboolean not null
  • paused_untildate
  • created_attimestamptz not null

shipments

  • iduuid not null
  • subscription_id> subscriptionsuuid not null
  • box_id> boxesuuid not null
  • tracking_numbertext
  • shipped_attimestamptz
  • 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 "box_items" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "box_id" UUID NOT NULL,
  "product_id" UUID NOT NULL,
  "quantity" INTEGER DEFAULT 1 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "box_plans" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "price" NUMERIC(8,2) NOT NULL,
  "frequency" TEXT DEFAULT 'monthly' NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "boxes" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "plan_id" UUID NOT NULL,
  "month" DATE NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

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

CREATE TABLE "products" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "cost" NUMERIC(8,2) NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "shipments" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "subscription_id" UUID NOT NULL,
  "box_id" UUID NOT NULL,
  "tracking_number" TEXT,
  "shipped_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "subscriptions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "customer_id" UUID NOT NULL,
  "plan_id" UUID NOT NULL,
  "active" BOOLEAN DEFAULT true NOT NULL,
  "paused_until" DATE,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "boxes" ADD CONSTRAINT "fk_boxes_plan_id" FOREIGN KEY ("plan_id") REFERENCES "box_plans" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "box_items" ADD CONSTRAINT "fk_box_items_box_id" FOREIGN KEY ("box_id") REFERENCES "boxes" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

ALTER TABLE "subscriptions" ADD CONSTRAINT "fk_subscriptions_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "subscriptions" ADD CONSTRAINT "fk_subscriptions_plan_id" FOREIGN KEY ("plan_id") REFERENCES "box_plans" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "shipments" ADD CONSTRAINT "fk_shipments_subscription_id" FOREIGN KEY ("subscription_id") REFERENCES "subscriptions" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "shipments" ADD CONSTRAINT "fk_shipments_box_id" FOREIGN KEY ("box_id") REFERENCES "boxes" ("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.