Templates/Commerce

Product catalogue with variants database schema

Products with options (size, colour), variants, images and nested categories.

7 tables, 9 relationships

Opens as a new diagram you can change. Free.

Tables

categories

  • iduuid not null
  • nametext not null
  • parent_id> categoriesuuid
  • created_attimestamptz not null

products

  • iduuid not null
  • category_id> categoriesuuid
  • nametext not null
  • descriptiontext
  • slugtext not null
  • created_attimestamptz not null

product_options

  • iduuid not null
  • product_id> productsuuid not null
  • nametext not null
  • positioninteger not null
  • created_attimestamptz not null

option_values

  • iduuid not null
  • option_id> product_optionsuuid not null
  • valuetext not null
  • created_attimestamptz not null

variants

  • iduuid not null
  • product_id> productsuuid not null
  • skutext not null
  • pricenumeric(10,2) not null
  • stockinteger not null
  • created_attimestamptz not null

variant_option_values

  • iduuid not null
  • variant_id> variantsuuid not null
  • option_value_id> option_valuesuuid not null
  • created_attimestamptz not null

product_images

  • iduuid not null
  • product_id> productsuuid not null
  • variant_id> variantsuuid
  • urltext not null
  • alttext
  • 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 "categories" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "parent_id" UUID,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "option_values" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "option_id" UUID NOT NULL,
  "value" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "product_images" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "product_id" UUID NOT NULL,
  "variant_id" UUID,
  "url" TEXT NOT NULL,
  "alt" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "product_options" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "product_id" UUID NOT NULL,
  "name" TEXT NOT NULL,
  "position" INTEGER DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "products" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "category_id" UUID,
  "name" TEXT NOT NULL,
  "description" TEXT,
  "slug" TEXT NOT NULL UNIQUE,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "variant_option_values" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "variant_id" UUID NOT NULL,
  "option_value_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "variants" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "product_id" UUID NOT NULL,
  "sku" TEXT NOT NULL UNIQUE,
  "price" NUMERIC(10,2) NOT NULL,
  "stock" INTEGER DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "categories" ADD CONSTRAINT "fk_categories_parent_id" FOREIGN KEY ("parent_id") REFERENCES "categories" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "products" ADD CONSTRAINT "fk_products_category_id" FOREIGN KEY ("category_id") REFERENCES "categories" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

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

ALTER TABLE "option_values" ADD CONSTRAINT "fk_option_values_option_id" FOREIGN KEY ("option_id") REFERENCES "product_options" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

ALTER TABLE "variant_option_values" ADD CONSTRAINT "fk_variant_option_values_variant_id" FOREIGN KEY ("variant_id") REFERENCES "variants" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "variant_option_values" ADD CONSTRAINT "fk_variant_option_values_option_value_id" FOREIGN KEY ("option_value_id") REFERENCES "option_values" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

ALTER TABLE "product_images" ADD CONSTRAINT "fk_product_images_variant_id" FOREIGN KEY ("variant_id") REFERENCES "variants" ("id") ON DELETE SET NULL 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.