Templates/Social & community

Social network database schema

Profiles, follows, posts with media, comments and likes.

6 tables, 9 relationships

Opens as a new diagram you can change. Free.

Tables

users

  • iduuid not null
  • usernametext not null
  • display_nametext not null
  • biotext
  • avatar_urltext
  • created_attimestamptz not null

follows

  • iduuid not null
  • follower_id> usersuuid not null
  • followee_id> usersuuid not null
  • created_attimestamptz not null

posts

  • iduuid not null
  • author_id> usersuuid not null
  • bodytext not null
  • reply_to_id> postsuuid
  • created_attimestamptz not null

post_media

  • iduuid not null
  • post_id> postsuuid not null
  • urltext not null
  • kindtext not null
  • created_attimestamptz not null

comments

  • iduuid not null
  • post_id> postsuuid not null
  • author_id> usersuuid not null
  • bodytext not null
  • created_attimestamptz not null

likes

  • iduuid not null
  • post_id> postsuuid not null
  • user_id> usersuuid 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 "comments" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "post_id" UUID NOT NULL,
  "author_id" UUID NOT NULL,
  "body" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "follows" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "follower_id" UUID NOT NULL,
  "followee_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

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

CREATE TABLE "post_media" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "post_id" UUID NOT NULL,
  "url" TEXT NOT NULL,
  "kind" TEXT 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,
  "body" TEXT NOT NULL,
  "reply_to_id" UUID,
  "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,
  "display_name" TEXT NOT NULL,
  "bio" TEXT,
  "avatar_url" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "follows" ADD CONSTRAINT "fk_follows_follower_id" FOREIGN KEY ("follower_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "follows" ADD CONSTRAINT "fk_follows_followee_id" FOREIGN KEY ("followee_id") REFERENCES "users" ("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 CASCADE ON UPDATE NO ACTION;

ALTER TABLE "posts" ADD CONSTRAINT "fk_posts_reply_to_id" FOREIGN KEY ("reply_to_id") REFERENCES "posts" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "post_media" ADD CONSTRAINT "fk_post_media_post_id" FOREIGN KEY ("post_id") REFERENCES "posts" ("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_author_id" FOREIGN KEY ("author_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

ALTER TABLE "likes" ADD CONSTRAINT "fk_likes_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE 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.