Social Media Database Schema database schema

The Social Media Database Schema database schema by Colton Bristow has 35 tables (users, profiles, sessions, follows and 31 more), 203 columns and 66 relationships, documented column by column.

ER diagram of the Social Media Database Schema schema, showing 35 tables and their relationships
FIG. 1: entity relationship diagram, 35 tables
Tables
35
Columns
203
Relationships
66
Enums
13

Tables (35)

Tables in the Social Media Database Schema schema
TableColumnsDescription
users9Handle and credentials only. Everything a visitor sees lives in profiles, so a rename never touches the auth row.
profiles9One-to-one with users. Split out because it is read on every profile view and written far more often than credentials.
sessions9One row per signed-in device. revoked_at null means live, which is what powers a log-out-everywhere button.
follows4Directed. Mutual following is two rows. status pending covers follow requests to a private account.
blocks3Blocking is mutual invisibility and should also delete any follows rows in both directions.
mutes3Unlike a block, a mute is invisible to the muted user and leaves the follow intact. It only filters the feed.
lists6
list_members3Membership does not require following, which is what makes lists useful for reading without subscribing.
posts14Self-referencing for threads. The three count columns are denormalised caches, rebuildable from the engagement tables. deleted_at is a soft delete so replies do not orphan.
post_media10Bytes live in object storage. This table holds the pointer and the metadata a client needs to lay out before the file loads.
hashtags4Stored once, lowercased. usage_count is a cache that drives trending.
post_hashtags2
post_mentions2Extracted at write time rather than parsed on read, so the mentions tab is a plain index lookup.
post_likes3The composite primary key is what makes a like idempotent. Never store a like as a counter alone.
reposts5A null comment is a plain repost; a non-null one is a quote. Both fan out to followers.
bookmarks3Private by definition. Never generates a notification.
comments9Separate from posts on purpose: comments are never independently followable and never enter the fan-out feed.
comment_likes3
polls5One poll per post. Results are counted from poll_votes, never stored on the option.
poll_options4
poll_votes3Single-choice polls add a partial unique index on (poll_id, user_id) through the option join.
stories7expires_at is written at creation, not inferred. The index on it is what lets a sweeper reap expired rows cheaply.
story_views3The composite key means a re-watch does not inflate the viewer list.
conversations6last_message_at is denormalised so the inbox sorts without touching messages.
conversation_participants6left_at null means currently in the thread. Keeping the row preserves who saw what before they left.
messages8The composite index on conversation and time is the single most important index in a chat system.
message_attachments6
message_reads4A high-water mark per participant, not a row per message. Unread count is a range query above this point.
feed_entries7The materialised timeline, written on fan-out. reason and source_user_id are what let the UI say why a post is here.
notifications8actor_id null is a system notification. The partial index on unread is what keeps the badge count cheap.
notification_preferences5One row per user per notification type. Absent row means fall back to the product default.
groups8
group_members4Posts carry a nullable group_id, so one posts table serves both the public timeline and every group.
reports9Exactly one of the three target columns is set, enforced by a check constraint.
moderation_actions9Append-only. A suspension is not a flag on users, it is a row here with an expires_at, so the history survives the appeal.

Enums (13)

account_status
active, suspended, deactivated, deleted
follow_status
pending, accepted
post_type
text, media, poll, repost, reply
post_visibility
public, followers, mentioned, group, private
media_type
image, video, gif, audio, file
conversation_type
direct, group
feed_reason
followed, reposted, liked_by_followee, group, recommended
notification_type
follow, follow_request, like, comment, reply, mention, repost, message, group_invite, system
group_privacy
public, closed, secret
group_role
owner, admin, moderator, member
report_reason
spam, harassment, hate_speech, violence, nudity, misinformation, impersonation, other
report_status
open, reviewing, actioned, dismissed
moderation_action_type
warn, remove_content, restrict, suspend, ban, no_action

Document your own schema

This page is published from DB Planner, a collaborative ER diagram tool. Write the schema in the DBML editor or import it from SQL, then publish docs like these from the project's sharing settings.

More published database schemas