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
- Discussion forumCategories, threads and posts with reactions and moderation reports.
- Chat / messagingDirect and group conversations, messages, attachments and read receipts.
- Dating appProfiles with photos, swipes, mutual matches, messages and blocks.
- Meetup groupsInterest groups with members, events at venues and RSVPs.
- Q&A siteQuestions, answers with votes, accepted answers and tags.
- Reviews & ratingsBusinesses reviewed by users, with photos, helpful votes and owner replies.
- Creator membershipsCreators offering paid tiers, supporters' pledges and members-only posts.
- GamificationPoints ledger, levels, badges and streaks for an engaged community.
- Mentorship programmeProgrammes matching mentors with mentees, sessions and feedback.
- Chat app (Firestore)Users, rooms with their members, and messages as a subcollection of each room.
- Activity feed (Cassandra)Each user's feed by time bucket, follower lists both ways, and like counters.
Start with one diagram, free
One diagram with two branches, no card needed. Upgrade when your team needs more.