Templates/Content & media

Blog / CMS database schema

Authors, posts, categories (many-to-many) and threaded comments.

5 tables, 5 relationships

Opens as a new diagram you can change. Free.

Tables

authors

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

posts

  • iduuid not null
  • author_id> authorsuuid not null
  • titletext not null
  • slugtext not null
  • bodytext not null
  • statuspost_status not null
  • published_attimestamptz
  • created_attimestamptz not null

categories

  • iduuid not null
  • nametext not null
  • slugtext not null
  • created_attimestamptz not null

post_categories

  • iduuid not null
  • post_id> postsuuid not null
  • category_id> categoriesuuid not null
  • created_attimestamptz not null

comments

  • iduuid not null
  • post_id> postsuuid not null
  • parent_id> commentsuuid
  • author_nametext not null
  • bodytext not null
  • approvedboolean 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 TYPE "post_status" AS ENUM ('draft', 'published', 'archived');

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

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

CREATE TABLE "comments" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "post_id" UUID NOT NULL,
  "parent_id" UUID,
  "author_name" TEXT NOT NULL,
  "body" TEXT NOT NULL,
  "approved" BOOLEAN DEFAULT false NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "post_categories" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "post_id" UUID NOT NULL,
  "category_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "posts" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "author_id" UUID NOT NULL,
  "title" TEXT NOT NULL,
  "slug" TEXT NOT NULL UNIQUE,
  "body" TEXT NOT NULL,
  "status" "post_status" DEFAULT 'draft' NOT NULL,
  "published_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "posts" ADD CONSTRAINT "fk_posts_author_id" FOREIGN KEY ("author_id") REFERENCES "authors" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "post_categories" ADD CONSTRAINT "fk_post_categories_post_id" FOREIGN KEY ("post_id") REFERENCES "posts" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "post_categories" ADD CONSTRAINT "fk_post_categories_category_id" FOREIGN KEY ("category_id") REFERENCES "categories" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "comments" ADD CONSTRAINT "fk_comments_post_id" FOREIGN KEY ("post_id") REFERENCES "posts" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "comments" ADD CONSTRAINT "fk_comments_parent_id" FOREIGN KEY ("parent_id") REFERENCES "comments" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

More Content & media templates

Start with one diagram, free

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