Templates/Social & community

Discussion forum database schema

Categories, threads and posts with reactions and moderation reports.

6 tables, 8 relationships

Opens as a new diagram you can change. Free.

Tables

users

  • iduuid not null
  • usernametext not null
  • emailtext not null
  • is_moderatorboolean not null
  • created_attimestamptz not null

categories

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

threads

  • iduuid not null
  • category_id> categoriesuuid not null
  • author_id> usersuuid not null
  • titletext not null
  • pinnedboolean not null
  • lockedboolean not null
  • created_attimestamptz not null

posts

  • iduuid not null
  • thread_id> threadsuuid not null
  • author_id> usersuuid not null
  • bodytext not null
  • edited_attimestamptz
  • created_attimestamptz not null

reactions

  • iduuid not null
  • post_id> postsuuid not null
  • user_id> usersuuid not null
  • emojitext not null
  • created_attimestamptz not null

reports

  • iduuid not null
  • post_id> postsuuid not null
  • reporter_id> usersuuid not null
  • reasontext not null
  • resolved_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 "categories" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "slug" TEXT NOT NULL UNIQUE,
  "position" INTEGER DEFAULT 0 NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "posts" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "thread_id" UUID NOT NULL,
  "author_id" UUID NOT NULL,
  "body" TEXT NOT NULL,
  "edited_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "reactions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "post_id" UUID NOT NULL,
  "user_id" UUID NOT NULL,
  "emoji" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "reports" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "post_id" UUID NOT NULL,
  "reporter_id" UUID NOT NULL,
  "reason" TEXT NOT NULL,
  "resolved_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "threads" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "category_id" UUID NOT NULL,
  "author_id" UUID NOT NULL,
  "title" TEXT NOT NULL,
  "pinned" BOOLEAN DEFAULT false NOT NULL,
  "locked" BOOLEAN DEFAULT false NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "users" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "username" TEXT NOT NULL UNIQUE,
  "email" TEXT NOT NULL UNIQUE,
  "is_moderator" BOOLEAN DEFAULT false NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "threads" ADD CONSTRAINT "fk_threads_category_id" FOREIGN KEY ("category_id") REFERENCES "categories" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "threads" ADD CONSTRAINT "fk_threads_author_id" FOREIGN KEY ("author_id") REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "posts" ADD CONSTRAINT "fk_posts_thread_id" FOREIGN KEY ("thread_id") REFERENCES "threads" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

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

ALTER TABLE "reactions" ADD CONSTRAINT "fk_reactions_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

ALTER TABLE "reports" ADD CONSTRAINT "fk_reports_reporter_id" FOREIGN KEY ("reporter_id") REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

More Social & community templates

Start with one diagram, free

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