Photo Sharing ER diagram and database schema
The Photo Sharing database schema is a DB Planner template with 28 tables, 192 columns and 60 relationships, free to use. Core tables include blocks, close_friends, collection_posts, collections. It can be opened as an interactive ER diagram or exported as PostgreSQL, MySQL, or SQL Server DDL.
- Tables
- 28
- Columns
- 192
- Relationships
- 60
- Category
- Social
- Exports
- DBML, SQL, Prisma, C#
- Price
- Free
What this schema models
A complete photo sharing app database design, the kind of data model behind an app similar to Instagram: 28 tables, 13 enums and 60 relationships covering accounts, profiles and follows (with requests for private accounts), carousel posts with media variants, hashtags, mentions and photo tags, likes, threaded comments and saved collections, expiring stories with views and highlights, notifications and push devices, and blocks, mutes, reports and moderation actions. THE FOUR DESIGN RULES 1. Counters are caches; rows are the truth. Like, comment, follower and hashtag counts are denormalized for fast feeds and profile headers, but post_likes, comments, follows and post_hashtags are authoritative. Each cache is incremented in the same transaction and reconciled by a background job; when the two disagree, the rows win. 2. A post is a container, media are files. The post holds the caption and settings; each photo or video is an ordered post_media row (that is all a carousel is), with resized and transcoded variants by storage key. Bytes never live in the database, and a phone on a slow network never downloads the original. 3. A follow request is a state, not a separate table. Following a private account inserts the same follows row in state pending; approving is an UPDATE, and every visibility check reads only accepted rows. 4. Expiry and removal are timestamps, not deletes. Stories carry expires_at and feeds filter on it, so an expired story can still go into a highlight. Moderators never hard-delete: posts, comments and stories point at the moderation action that removed them, and a successful appeal is a new action that reverses it. DETAILS WORTH STEALING - Photo tags store x and y as fractions of the image (0 to 1), so a tag lands on the same spot in every resized variant. - Hashtags are derived from the caption on every save: the caption is the source, post_hashtags is its index. - Replies are one level deep: replying to a reply points at the same top-level comment and mentions the person. - Collections are folders over saves, joined by a composite foreign key that cascades, so unsaving removes a post from every collection at once. - Notifications carry a group_key, so "sam and 41 others liked your post" is grouped at read time and an undone like deletes exactly its own row. - Blocks are indexed in both directions because every read path checks both; a mute is a quiet unfollow the muted person never sees. WHAT YOU GET Clean DBML that imports directly into DB Planner, and a laid-out board with zones color-coded and framed: accounts and follows, posts and media, engagement, stories, notifications, and trust and safety. Every table carries a note. Postgres-flavoured types that port cleanly to MySQL or SQL Server. Useful for developers building a photo sharing or social media app, and for students who need a realistic photo sharing app ER diagram for a DBMS project. This is a schema design reference; feed ranking, media processing and content policy belong in your own services.
Tables (28)
| Table | Columns |
|---|---|
| blocks | 3 |
| close_friends | 3 |
| collection_posts | 4 |
| collections | 5 |
| comment_likes | 3 |
| comments | 12 |
| follows | 5 |
| hashtags | 5 |
| highlight_stories | 3 |
| highlights | 6 |
| media_variants | 8 |
| mentions | 5 |
| moderation_actions | 13 |
| mutes | 5 |
| notifications | 10 |
| photo_tags | 6 |
| places | 7 |
| post_hashtags | 3 |
| post_likes | 3 |
| post_media | 11 |
| posts | 13 |
| profiles | 11 |
| push_devices | 7 |
| reports | 12 |
| saves | 3 |
| stories | 13 |
| story_views | 3 |
| users | 10 |
Every table and its columns
users
Login identity and account state. Everything public-facing lives on profiles, so credentials are never part of a query that renders a page.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| varchar(254) | unique | |||
| phone | varchar(30) | unique | E.164 format. An account needs an email or a phone (a CHECK constraint), not both | |
| password_hash | varchar(200) | Null for accounts that sign in only with a social login or a one-time code | ||
| status | account_status | not null, default 'active' | ||
| is_staff | boolean | not null, default false | Can work the moderation queue. A role on the same account, never a separate login | |
| date_of_birth | date | Minimum age checks and teen account defaults | ||
| created_at | timestamptz | not null, default `now()` | ||
| deactivated_at | timestamptz | User-initiated pause: content hidden, fully restorable | ||
| deleted_at | timestamptz | Soft delete. A purge job erases personal data after the grace period; the row then keeps only its id |
profiles
The public face of an account, one to one with users. The counters are denormalized for profile headers and reconciled from their source rows by a background job.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| user_id | uuid | PK, FK | users.id | |
| username | varchar(30) | unique, not null | Stored lowercase; uniqueness is case-insensitive (citext, or a unique index on lower(username)) | |
| display_name | varchar(64) | |||
| bio | varchar(150) | |||
| website_url | varchar(300) | |||
| avatar_storage_key | varchar(300) | Object storage key. Avatar sizes are derived by naming convention | ||
| is_private | boolean | not null, default false | Private accounts turn every new follow into a request | |
| follower_count | integer | not null, default 0 | CACHE. Authoritative: accepted rows in follows | |
| following_count | integer | not null, default 0 | CACHE. Authoritative: accepted rows in follows | |
| post_count | integer | not null, default 0 | CACHE. Authoritative: live rows in posts | |
| updated_at | timestamptz | not null, default `now()` |
follows
One row per directed follow. A request to a private account is the same row in state pending, so approving is an UPDATE and every visibility check reads only state = accepted. Declining deletes the row.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| follower_id | uuid | FK, not null | users.id | |
| followee_id | uuid | FK, not null | users.id | |
| state | follow_state | not null | pending until a private account approves; accepted straight away otherwise | |
| requested_at | timestamptz | not null, default `now()` | ||
| accepted_at | timestamptz |
Indexes: (followee_id, state)
close_friends
A private audience list for stories. The friend is never told they were added or removed.
places
Locations a post can be tagged at. A place page lists posts by place_id, so coordinates are stored once.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| name | varchar(200) | not null | ||
| latitude | numeric(9,6) | not null | ||
| longitude | numeric(9,6) | not null | ||
| city | varchar(120) | |||
| country | char(2) | |||
| provider_place_id | varchar(100) | unique | Id from the maps provider the location was picked from, so re-picking it finds this row |
posts
A post is a container: caption, place and settings. The photos and videos are post_media rows, which is what makes a carousel simply a post with more than one media row.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| author_id | uuid | FK, not null | users.id | |
| caption | text | Raw text as typed. Hashtags and mentions are parsed out of it on every save | ||
| place_id | uuid | FK | places.id | |
| status | post_status | not null, default 'processing' | ||
| comments_disabled | boolean | not null, default false | ||
| hide_like_count | boolean | not null, default false | ||
| like_count | integer | not null, default 0 | CACHE of count(post_likes) | |
| comment_count | integer | not null, default 0 | CACHE of live comments | |
| created_at | timestamptz | not null, default `now()` | ||
| edited_at | timestamptz | |||
| deleted_at | timestamptz | Soft delete by the author | ||
| removed_by_action_id | uuid | FK | moderation_actions.id | Set when moderation removes the post. Clearing it restores the post exactly as it was |
Indexes: (author_id, created_at)
post_media
One photo or video in a post, in carousel order. Bytes live in object storage; the database holds keys and metadata only.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| post_id | uuid | FK, not null | posts.id | |
| position | smallint | not null | 0-based order in the carousel | |
| media_type | media_type | not null | ||
| original_storage_key | varchar(300) | not null | The upload as received. Kept private; nothing serves it directly | |
| width | integer | not null | ||
| height | integer | not null | ||
| duration_ms | integer | Videos only | ||
| alt_text | varchar(500) | Written by the author or generated; read aloud by screen readers | ||
| blurhash | varchar(60) | Tiny placeholder string the app paints while the real image loads | ||
| processing_status | processing_status | not null, default 'pending' |
Indexes: (post_id, position) unique
media_variants
Resized and transcoded renditions written by the media pipeline. A feed asks for the variant that fits the screen, so a phone on a slow network never downloads the original.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| post_media_id | uuid | FK, not null | post_media.id | |
| variant | media_variant | not null | ||
| content_type | varchar(50) | not null | e.g. image/webp, image/jpeg, video/mp4 | |
| storage_key | varchar(300) | unique, not null | ||
| width | integer | not null | ||
| height | integer | not null | ||
| bytes | integer | not null |
Indexes: (post_media_id, variant, content_type) unique
photo_tags
People tagged ON a photo, at a point. Different from a mention, which lives in text.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| post_media_id | uuid | FK, not null | post_media.id | |
| tagged_user_id | uuid | FK, not null | users.id | |
| x | numeric(5,4) | not null | 0 to 1, a fraction of the width, so the tag lands on the same spot in every variant | |
| y | numeric(5,4) | not null | 0 to 1, a fraction of the height | |
| approved | boolean | not null, default true | False while waiting, for users who review tags manually | |
| created_at | timestamptz | not null, default `now()` |
Indexes: (tagged_user_id)
hashtags
One row per distinct tag ever used.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| name | varchar(100) | unique, not null | Lowercased, without the #. Display casing comes from the caption itself | |
| post_count | integer | not null, default 0 | CACHE of post_hashtags rows | |
| is_blocked | boolean | not null, default false | Blocked tags are not searchable and their page shows nothing | |
| created_at | timestamptz | not null, default `now()` |
post_hashtags
Derived from the caption on every save: the caption is the source, these rows are its index. Editing a caption rewrites the set.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| post_id | uuid | FK, not null | posts.id | |
| hashtag_id | uuid | FK, not null | hashtags.id | |
| created_at | timestamptz | not null | Copied from the post, so a hashtag page sorts by time without joining posts |
Indexes: (hashtag_id, created_at)
mentions
@username references parsed from a caption or a comment. Exactly one of post_id and comment_id is set (a CHECK constraint). Stored by user id, so a username change never breaks the link.
| Column | Type | Constraints | References |
|---|---|---|---|
| id | uuid | PK | |
| mentioned_user_id | uuid | FK, not null | users.id |
| post_id | uuid | FK | posts.id |
| comment_id | uuid | FK | comments.id |
| created_at | timestamptz | not null, default `now()` |
Indexes: (mentioned_user_id)
post_likes
The authoritative likes. posts.like_count is a cache incremented in the same transaction and reconciled from these rows; when the two disagree, these rows win.
| Column | Type | Constraints | References |
|---|---|---|---|
| post_id | uuid | FK, not null | posts.id |
| user_id | uuid | FK, not null | users.id |
| created_at | timestamptz | not null, default `now()` |
Indexes: (user_id, created_at)
comments
Threaded comments. A soft-deleted parent keeps its replies attached instead of orphaning them.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| post_id | uuid | FK, not null | posts.id | |
| author_id | uuid | FK, not null | users.id | |
| parent_comment_id | uuid | FK | comments.id | Null for a top-level comment. Replies are one level deep: replying to a reply points at the same top-level comment and mentions the person |
| body | varchar(2200) | not null | ||
| like_count | integer | not null, default 0 | CACHE of comment_likes | |
| reply_count | integer | not null, default 0 | CACHE of live replies | |
| is_pinned | boolean | not null, default false | The post author can pin a few comments to the top | |
| created_at | timestamptz | not null, default `now()` | ||
| edited_at | timestamptz | |||
| deleted_at | timestamptz | Soft delete. Replies stay attached and the parent renders as deleted | ||
| removed_by_action_id | uuid | FK | moderation_actions.id |
Indexes: (post_id, parent_comment_id, created_at)
comment_likes
Likes on comments. comments.like_count is their cache.
| Column | Type | Constraints | References |
|---|---|---|---|
| comment_id | uuid | FK, not null | comments.id |
| user_id | uuid | FK, not null | users.id |
| created_at | timestamptz | not null, default `now()` |
saves
Bookmarks. Private to the saver; the author only ever sees an aggregate count.
| Column | Type | Constraints | References |
|---|---|---|---|
| user_id | uuid | FK, not null | users.id |
| post_id | uuid | FK, not null | posts.id |
| saved_at | timestamptz | not null, default `now()` |
Indexes: (user_id, saved_at)
collections
Optional named folders over saved posts.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| owner_id | uuid | FK, not null | users.id | |
| name | varchar(100) | not null | ||
| cover_post_id | uuid | FK | posts.id | Null means use the most recently added post |
| created_at | timestamptz | not null, default `now()` |
Indexes: (owner_id, name) unique
collection_posts
Which saved posts are filed in which collection. The composite foreign key to saves cascades, so unsaving a post removes it from every collection at once.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| collection_id | uuid | FK, not null | collections.id | |
| user_id | uuid | FK, not null | saves.user_id | The collection owner, repeated so the row can reference the save it files |
| post_id | uuid | not null | ||
| added_at | timestamptz | not null, default `now()` |
stories
Ephemeral posts. An expired story leaves the tray but stays in the author archive, which is how it can still be added to a highlight.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| author_id | uuid | FK, not null | users.id | |
| media_type | media_type | not null | ||
| storage_key | varchar(300) | not null | ||
| width | integer | not null | ||
| height | integer | not null | ||
| duration_ms | integer | not null, default 5000 | How long it shows. Videos use their own length, up to a cap | |
| audience | story_audience | not null, default 'followers' | ||
| view_count | integer | not null, default 0 | CACHE of story_views | |
| created_at | timestamptz | not null, default `now()` | ||
| expires_at | timestamptz | not null | created_at + 24 hours. Feeds filter on it; no job deletes anything at expiry | |
| deleted_at | timestamptz | |||
| removed_by_action_id | uuid | FK | moderation_actions.id |
Indexes: (author_id, expires_at)
story_views
Who saw a story, visible only to its author. Purged a couple of days after expiry; the archive keeps stories.view_count.
| Column | Type | Constraints | References |
|---|---|---|---|
| story_id | uuid | FK, not null | stories.id |
| viewer_id | uuid | FK, not null | users.id |
| viewed_at | timestamptz | not null, default `now()` |
highlights
Named sets of past stories pinned to a profile. They outlive expiry because they point at story rows that were never deleted.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| owner_id | uuid | FK, not null | users.id | |
| title | varchar(30) | not null | ||
| cover_storage_key | varchar(300) | |||
| position | smallint | not null | Order on the profile | |
| created_at | timestamptz | not null, default `now()` |
highlight_stories
Which stories are in a highlight, in order.
| Column | Type | Constraints | References |
|---|---|---|---|
| highlight_id | uuid | FK, not null | highlights.id |
| story_id | uuid | FK, not null | stories.id |
| position | smallint | not null |
notifications
The in-app activity feed. One row per event, grouped at read time, so an undone like deletes exactly its own row.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| recipient_id | uuid | FK, not null | users.id | |
| actor_id | uuid | FK | users.id | Who did it; null for system notices |
| kind | notification_kind | not null | ||
| post_id | uuid | FK | posts.id | |
| comment_id | uuid | FK | comments.id | |
| story_id | uuid | FK | stories.id | |
| group_key | varchar(120) | e.g. like:post:<id>. The inbox collapses rows sharing a key into "sam and 41 others liked your post" | ||
| created_at | timestamptz | not null, default `now()` | ||
| read_at | timestamptz |
Indexes: (recipient_id, created_at), (recipient_id, group_key)
push_devices
Where to deliver push notifications. One user, many phones and tablets.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| user_id | uuid | FK, not null | users.id | |
| platform | device_platform | not null | ||
| push_token | varchar(500) | unique, not null | Issued by the OS push service. It rotates, so the app upserts it on every launch | |
| app_version | varchar(20) | |||
| last_seen_at | timestamptz | not null | ||
| disabled_at | timestamptz | Set when the push service reports the token invalid |
blocks
A block removes any follow in both directions and hides each person from the other everywhere: feed, search, comments, stories. Every read path filters on it.
| Column | Type | Constraints | References |
|---|---|---|---|
| blocker_id | uuid | FK, not null | users.id |
| blocked_id | uuid | FK, not null | users.id |
| created_at | timestamptz | not null, default `now()` |
Indexes: (blocked_id)
mutes
A quiet unfollow: the follow stays, the content stops appearing, and the muted person is never told.
reports
User reports. Many reports about one post are many rows; the queue groups them by target so a moderator reviews it once.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| reporter_id | uuid | FK, not null | users.id | |
| target_kind | report_target | not null | ||
| reported_user_id | uuid | FK, not null | users.id | Always set: the author of the reported content, or the account itself |
| post_id | uuid | FK | posts.id | |
| comment_id | uuid | FK | comments.id | |
| story_id | uuid | FK | stories.id | |
| reason | report_reason | not null | ||
| details | text | |||
| status | report_status | not null, default 'open' | ||
| created_at | timestamptz | not null, default `now()` | ||
| resolved_at | timestamptz |
Indexes: (status, created_at), (reported_user_id)
moderation_actions
Append-only log of everything moderation did. A moderator never hard-deletes content: posts, comments and stories point back here through removed_by_action_id.
| Column | Type | Constraints | References | Description |
|---|---|---|---|---|
| id | uuid | PK | ||
| report_id | uuid | FK | reports.id | Null for proactive actions, e.g. from an automated classifier |
| moderator_id | uuid | FK | users.id | A staff user; null when an automated system acted |
| action | moderation_action_kind | not null | ||
| target_user_id | uuid | FK, not null | users.id | |
| post_id | uuid | FK | posts.id | |
| comment_id | uuid | FK | comments.id | |
| story_id | uuid | FK | stories.id | |
| policy_code | varchar(50) | not null | Which rule was broken, e.g. HARASSMENT_2. Quoted in the notice the user receives | |
| reason | text | |||
| expires_at | timestamptz | For time-limited suspensions | ||
| reverses_action_id | uuid | FK | moderation_actions.id | A successful appeal is a new restore action pointing at the one it undoes |
| created_at | timestamptz | not null, default `now()` |
Enums (13)
- account_status
- active, deactivated, suspended, banned, deleted
- follow_state
- pending, accepted
- post_status
- processing, published, archived
- media_type
- photo, video
- processing_status
- pending, processing, ready, failed
- media_variant
- thumbnail_150, small_320, medium_640, large_1080, video_480p, video_720p, video_1080p
- story_audience
- followers, close_friends
- notification_kind
- like, comment, reply, comment_like, mention, photo_tag, follow, follow_request, follow_accepted, moderation_notice
- device_platform
- ios, android, web
- report_target
- post, comment, story, account
- report_reason
- spam, harassment, hate_speech, nudity, violence, self_harm, impersonation, scam, intellectual_property, other
- report_status
- open, in_review, actioned, dismissed
- moderation_action_kind
- remove_content, restore_content, warn_user, suspend_user, ban_user, reinstate_user
Relationships (60)
blocks.blocked_id references users.idmany to oneblocks.blocker_id references users.idmany to oneclose_friends.friend_id references users.idmany to oneclose_friends.owner_id references users.idmany to onecollection_posts.collection_id references collections.idmany to onecollection_posts.user_id references saves.user_idmany to onecollections.cover_post_id references posts.idmany to onecollections.owner_id references users.idmany to onecomment_likes.comment_id references comments.idmany to onecomment_likes.user_id references users.idmany to onecomments.author_id references users.idmany to onecomments.parent_comment_id references comments.idmany to onecomments.post_id references posts.idmany to onecomments.removed_by_action_id references moderation_actions.idmany to onefollows.followee_id references users.idmany to onefollows.follower_id references users.idmany to onehighlight_stories.highlight_id references highlights.idmany to onehighlight_stories.story_id references stories.idmany to onehighlights.owner_id references users.idmany to onemedia_variants.post_media_id references post_media.idmany to onementions.comment_id references comments.idmany to onementions.mentioned_user_id references users.idmany to onementions.post_id references posts.idmany to onemoderation_actions.comment_id references comments.idmany to onemoderation_actions.moderator_id references users.idmany to onemoderation_actions.post_id references posts.idmany to onemoderation_actions.report_id references reports.idmany to onemoderation_actions.reverses_action_id references moderation_actions.idmany to onemoderation_actions.story_id references stories.idmany to onemoderation_actions.target_user_id references users.idmany to onemutes.muted_id references users.idmany to onemutes.muter_id references users.idmany to onenotifications.actor_id references users.idmany to onenotifications.comment_id references comments.idmany to onenotifications.post_id references posts.idmany to onenotifications.recipient_id references users.idmany to onenotifications.story_id references stories.idmany to onephoto_tags.post_media_id references post_media.idmany to onephoto_tags.tagged_user_id references users.idmany to onepost_hashtags.hashtag_id references hashtags.idmany to onepost_hashtags.post_id references posts.idmany to onepost_likes.post_id references posts.idmany to onepost_likes.user_id references users.idmany to onepost_media.post_id references posts.idmany to oneposts.author_id references users.idmany to oneposts.place_id references places.idmany to oneposts.removed_by_action_id references moderation_actions.idmany to onepush_devices.user_id references users.idmany to onereports.comment_id references comments.idmany to onereports.post_id references posts.idmany to onereports.reported_user_id references users.idmany to onereports.reporter_id references users.idmany to onereports.story_id references stories.idmany to onesaves.post_id references posts.idmany to onesaves.user_id references users.idmany to onestories.author_id references users.idmany to onestories.removed_by_action_id references moderation_actions.idmany to onestory_views.story_id references stories.idmany to onestory_views.viewer_id references users.idmany to oneusers.id references profiles.user_idone to one
Photo Sharing database schema in DBML
The schema as it is written: DBML, the text format DB Planner draws its diagrams from. Copy it into any DBML tool, or open it in the diagram above.
// Photo Sharing
// Zones (header colors): accounts and follows (blue), posts and media (green),
// engagement (amber), stories (purple), notifications (gray),
// trust and safety (coral).
// ---------------------------------------------------------------------------
// ACCOUNTS AND FOLLOWS
// ---------------------------------------------------------------------------
Table users [headercolor: #4c8bf5] {
id uuid [pk]
email varchar(254) [unique]
phone varchar(30) [unique, note: 'E.164 format. An account needs an email or a phone (a CHECK constraint), not both']
password_hash varchar(200) [note: 'Null for accounts that sign in only with a social login or a one-time code']
status account_status [not null, default: 'active']
is_staff boolean [not null, default: false, note: 'Can work the moderation queue. A role on the same account, never a separate login']
date_of_birth date [note: 'Minimum age checks and teen account defaults']
created_at timestamptz [not null, default: `now()`]
deactivated_at timestamptz [note: 'User-initiated pause: content hidden, fully restorable']
deleted_at timestamptz [note: 'Soft delete. A purge job erases personal data after the grace period; the row then keeps only its id']
Note: 'Login identity and account state. Everything public-facing lives on profiles, so credentials are never part of a query that renders a page.'
}
Table profiles [headercolor: #4c8bf5] {
user_id uuid [pk, ref: - users.id]
username varchar(30) [not null, unique, note: 'Stored lowercase; uniqueness is case-insensitive (citext, or a unique index on lower(username))']
display_name varchar(64)
bio varchar(150)
website_url varchar(300)
avatar_storage_key varchar(300) [note: 'Object storage key. Avatar sizes are derived by naming convention']
is_private boolean [not null, default: false, note: 'Private accounts turn every new follow into a request']
follower_count integer [not null, default: 0, note: 'CACHE. Authoritative: accepted rows in follows']
following_count integer [not null, default: 0, note: 'CACHE. Authoritative: accepted rows in follows']
post_count integer [not null, default: 0, note: 'CACHE. Authoritative: live rows in posts']
updated_at timestamptz [not null, default: `now()`]
Note: 'The public face of an account, one to one with users. The counters are denormalized for profile headers and reconciled from their source rows by a background job.'
}
Table follows [headercolor: #4c8bf5] {
follower_id uuid [not null, ref: > users.id]
followee_id uuid [not null, ref: > users.id]
state follow_state [not null, note: 'pending until a private account approves; accepted straight away otherwise']
requested_at timestamptz [not null, default: `now()`]
accepted_at timestamptz
indexes {
(follower_id, followee_id) [pk]
(followee_id, state) [note: 'Followers list, and the pending requests inbox']
}
Note: 'One row per directed follow. A request to a private account is the same row in state pending, so approving is an UPDATE and every visibility check reads only state = accepted. Declining deletes the row.'
}
Table close_friends [headercolor: #4c8bf5] {
owner_id uuid [not null, ref: > users.id]
friend_id uuid [not null, ref: > users.id]
added_at timestamptz [not null, default: `now()`]
indexes {
(owner_id, friend_id) [pk]
}
Note: 'A private audience list for stories. The friend is never told they were added or removed.'
}
// ---------------------------------------------------------------------------
// POSTS AND MEDIA: the post is a container, the media are files
// ---------------------------------------------------------------------------
Table places [headercolor: #8bc34a] {
id uuid [pk]
name varchar(200) [not null]
latitude numeric(9,6) [not null]
longitude numeric(9,6) [not null]
city varchar(120)
country char(2)
provider_place_id varchar(100) [unique, note: 'Id from the maps provider the location was picked from, so re-picking it finds this row']
Note: 'Locations a post can be tagged at. A place page lists posts by place_id, so coordinates are stored once.'
}
Table posts [headercolor: #8bc34a] {
id uuid [pk]
author_id uuid [not null, ref: > users.id]
caption text [note: 'Raw text as typed. Hashtags and mentions are parsed out of it on every save']
place_id uuid [ref: > places.id]
status post_status [not null, default: 'processing']
comments_disabled boolean [not null, default: false]
hide_like_count boolean [not null, default: false]
like_count integer [not null, default: 0, note: 'CACHE of count(post_likes)']
comment_count integer [not null, default: 0, note: 'CACHE of live comments']
created_at timestamptz [not null, default: `now()`]
edited_at timestamptz
deleted_at timestamptz [note: 'Soft delete by the author']
removed_by_action_id uuid [ref: > moderation_actions.id, note: 'Set when moderation removes the post. Clearing it restores the post exactly as it was']
indexes {
(author_id, created_at) [note: 'Profile grid, newest first']
}
Note: 'A post is a container: caption, place and settings. The photos and videos are post_media rows, which is what makes a carousel simply a post with more than one media row.'
}
Table post_media [headercolor: #8bc34a] {
id uuid [pk]
post_id uuid [not null, ref: > posts.id]
position smallint [not null, note: '0-based order in the carousel']
media_type media_type [not null]
original_storage_key varchar(300) [not null, note: 'The upload as received. Kept private; nothing serves it directly']
width integer [not null]
height integer [not null]
duration_ms integer [note: 'Videos only']
alt_text varchar(500) [note: 'Written by the author or generated; read aloud by screen readers']
blurhash varchar(60) [note: 'Tiny placeholder string the app paints while the real image loads']
processing_status processing_status [not null, default: 'pending']
indexes {
(post_id, position) [unique]
}
Note: 'One photo or video in a post, in carousel order. Bytes live in object storage; the database holds keys and metadata only.'
}
Table media_variants [headercolor: #8bc34a] {
id uuid [pk]
post_media_id uuid [not null, ref: > post_media.id]
variant media_variant [not null]
content_type varchar(50) [not null, note: 'e.g. image/webp, image/jpeg, video/mp4']
storage_key varchar(300) [not null, unique]
width integer [not null]
height integer [not null]
bytes integer [not null]
indexes {
(post_media_id, variant, content_type) [unique]
}
Note: 'Resized and transcoded renditions written by the media pipeline. A feed asks for the variant that fits the screen, so a phone on a slow network never downloads the original.'
}
Table photo_tags [headercolor: #8bc34a] {
post_media_id uuid [not null, ref: > post_media.id]
tagged_user_id uuid [not null, ref: > users.id]
x numeric(5,4) [not null, note: '0 to 1, a fraction of the width, so the tag lands on the same spot in every variant']
y numeric(5,4) [not null, note: '0 to 1, a fraction of the height']
approved boolean [not null, default: true, note: 'False while waiting, for users who review tags manually']
created_at timestamptz [not null, default: `now()`]
indexes {
(post_media_id, tagged_user_id) [pk]
tagged_user_id [note: 'The tagged tab on a profile']
}
Note: 'People tagged ON a photo, at a point. Different from a mention, which lives in text.'
}
Table hashtags [headercolor: #8bc34a] {
id uuid [pk]
name varchar(100) [not null, unique, note: 'Lowercased, without the #. Display casing comes from the caption itself']
post_count integer [not null, default: 0, note: 'CACHE of post_hashtags rows']
is_blocked boolean [not null, default: false, note: 'Blocked tags are not searchable and their page shows nothing']
created_at timestamptz [not null, default: `now()`]
Note: 'One row per distinct tag ever used.'
}
Table post_hashtags [headercolor: #8bc34a] {
post_id uuid [not null, ref: > posts.id]
hashtag_id uuid [not null, ref: > hashtags.id]
created_at timestamptz [not null, note: 'Copied from the post, so a hashtag page sorts by time without joining posts']
indexes {
(post_id, hashtag_id) [pk]
(hashtag_id, created_at) [note: 'The hashtag page, newest first']
}
Note: 'Derived from the caption on every save: the caption is the source, these rows are its index. Editing a caption rewrites the set.'
}
Table mentions [headercolor: #8bc34a] {
id uuid [pk]
mentioned_user_id uuid [not null, ref: > users.id]
post_id uuid [ref: > posts.id]
comment_id uuid [ref: > comments.id]
created_at timestamptz [not null, default: `now()`]
indexes {
mentioned_user_id
}
Note: '@username references parsed from a caption or a comment. Exactly one of post_id and comment_id is set (a CHECK constraint). Stored by user id, so a username change never breaks the link.'
}
// ---------------------------------------------------------------------------
// ENGAGEMENT: likes, comments, saves
// ---------------------------------------------------------------------------
Table post_likes [headercolor: #f5b74c] {
post_id uuid [not null, ref: > posts.id]
user_id uuid [not null, ref: > users.id]
created_at timestamptz [not null, default: `now()`]
indexes {
(post_id, user_id) [pk, note: 'A double-tap twice is still one like']
(user_id, created_at) [note: 'Posts you have liked']
}
Note: 'The authoritative likes. posts.like_count is a cache incremented in the same transaction and reconciled from these rows; when the two disagree, these rows win.'
}
Table comments [headercolor: #f5b74c] {
id uuid [pk]
post_id uuid [not null, ref: > posts.id]
author_id uuid [not null, ref: > users.id]
parent_comment_id uuid [ref: > comments.id, note: 'Null for a top-level comment. Replies are one level deep: replying to a reply points at the same top-level comment and mentions the person']
body varchar(2200) [not null]
like_count integer [not null, default: 0, note: 'CACHE of comment_likes']
reply_count integer [not null, default: 0, note: 'CACHE of live replies']
is_pinned boolean [not null, default: false, note: 'The post author can pin a few comments to the top']
created_at timestamptz [not null, default: `now()`]
edited_at timestamptz
deleted_at timestamptz [note: 'Soft delete. Replies stay attached and the parent renders as deleted']
removed_by_action_id uuid [ref: > moderation_actions.id]
indexes {
(post_id, parent_comment_id, created_at) [note: 'Top-level thread, then each reply thread, in order']
}
Note: 'Threaded comments. A soft-deleted parent keeps its replies attached instead of orphaning them.'
}
Table comment_likes [headercolor: #f5b74c] {
comment_id uuid [not null, ref: > comments.id]
user_id uuid [not null, ref: > users.id]
created_at timestamptz [not null, default: `now()`]
indexes {
(comment_id, user_id) [pk]
}
Note: 'Likes on comments. comments.like_count is their cache.'
}
Table saves [headercolor: #f5b74c] {
user_id uuid [not null, ref: > users.id]
post_id uuid [not null, ref: > posts.id]
saved_at timestamptz [not null, default: `now()`]
indexes {
(user_id, post_id) [pk]
(user_id, saved_at) [note: 'All saved posts, newest first']
}
Note: 'Bookmarks. Private to the saver; the author only ever sees an aggregate count.'
}
Table collections [headercolor: #f5b74c] {
id uuid [pk]
owner_id uuid [not null, ref: > users.id]
name varchar(100) [not null]
cover_post_id uuid [ref: > posts.id, note: 'Null means use the most recently added post']
created_at timestamptz [not null, default: `now()`]
indexes {
(owner_id, name) [unique]
}
Note: 'Optional named folders over saved posts.'
}
Table collection_posts [headercolor: #f5b74c] {
collection_id uuid [not null, ref: > collections.id]
user_id uuid [not null, note: 'The collection owner, repeated so the row can reference the save it files']
post_id uuid [not null]
added_at timestamptz [not null, default: `now()`]
indexes {
(collection_id, post_id) [pk]
}
Note: 'Which saved posts are filed in which collection. The composite foreign key to saves cascades, so unsaving a post removes it from every collection at once.'
}
Ref: collection_posts.(user_id, post_id) > saves.(user_id, post_id) [delete: cascade]
// ---------------------------------------------------------------------------
// STORIES: expiry is a timestamp, not a delete
// ---------------------------------------------------------------------------
Table stories [headercolor: #b98cf0] {
id uuid [pk]
author_id uuid [not null, ref: > users.id]
media_type media_type [not null]
storage_key varchar(300) [not null]
width integer [not null]
height integer [not null]
duration_ms integer [not null, default: 5000, note: 'How long it shows. Videos use their own length, up to a cap']
audience story_audience [not null, default: 'followers']
view_count integer [not null, default: 0, note: 'CACHE of story_views']
created_at timestamptz [not null, default: `now()`]
expires_at timestamptz [not null, note: 'created_at + 24 hours. Feeds filter on it; no job deletes anything at expiry']
deleted_at timestamptz
removed_by_action_id uuid [ref: > moderation_actions.id]
indexes {
(author_id, expires_at) [note: 'The stories tray: WHERE expires_at > now()']
}
Note: 'Ephemeral posts. An expired story leaves the tray but stays in the author archive, which is how it can still be added to a highlight.'
}
Table story_views [headercolor: #b98cf0] {
story_id uuid [not null, ref: > stories.id]
viewer_id uuid [not null, ref: > users.id]
viewed_at timestamptz [not null, default: `now()`]
indexes {
(story_id, viewer_id) [pk, note: 'A rewatch is still one view']
}
Note: 'Who saw a story, visible only to its author. Purged a couple of days after expiry; the archive keeps stories.view_count.'
}
Table highlights [headercolor: #b98cf0] {
id uuid [pk]
owner_id uuid [not null, ref: > users.id]
title varchar(30) [not null]
cover_storage_key varchar(300)
position smallint [not null, note: 'Order on the profile']
created_at timestamptz [not null, default: `now()`]
Note: 'Named sets of past stories pinned to a profile. They outlive expiry because they point at story rows that were never deleted.'
}
Table highlight_stories [headercolor: #b98cf0] {
highlight_id uuid [not null, ref: > highlights.id]
story_id uuid [not null, ref: > stories.id]
position smallint [not null]
indexes {
(highlight_id, story_id) [pk]
}
Note: 'Which stories are in a highlight, in order.'
}
// ---------------------------------------------------------------------------
// NOTIFICATIONS
// ---------------------------------------------------------------------------
Table notifications [headercolor: #90a4ae] {
id uuid [pk]
recipient_id uuid [not null, ref: > users.id]
actor_id uuid [ref: > users.id, note: 'Who did it; null for system notices']
kind notification_kind [not null]
post_id uuid [ref: > posts.id]
comment_id uuid [ref: > comments.id]
story_id uuid [ref: > stories.id]
group_key varchar(120) [note: 'e.g. like:post:<id>. The inbox collapses rows sharing a key into "sam and 41 others liked your post"']
created_at timestamptz [not null, default: `now()`]
read_at timestamptz
indexes {
(recipient_id, created_at) [note: 'The activity inbox, newest first']
(recipient_id, group_key)
}
Note: 'The in-app activity feed. One row per event, grouped at read time, so an undone like deletes exactly its own row.'
}
Table push_devices [headercolor: #90a4ae] {
id uuid [pk]
user_id uuid [not null, ref: > users.id]
platform device_platform [not null]
push_token varchar(500) [not null, unique, note: 'Issued by the OS push service. It rotates, so the app upserts it on every launch']
app_version varchar(20)
last_seen_at timestamptz [not null]
disabled_at timestamptz [note: 'Set when the push service reports the token invalid']
Note: 'Where to deliver push notifications. One user, many phones and tablets.'
}
// ---------------------------------------------------------------------------
// TRUST AND SAFETY
// ---------------------------------------------------------------------------
Table blocks [headercolor: #f07178] {
blocker_id uuid [not null, ref: > users.id]
blocked_id uuid [not null, ref: > users.id]
created_at timestamptz [not null, default: `now()`]
indexes {
(blocker_id, blocked_id) [pk]
blocked_id [note: 'Blocks are checked in both directions, so both columns need an index']
}
Note: 'A block removes any follow in both directions and hides each person from the other everywhere: feed, search, comments, stories. Every read path filters on it.'
}
Table mutes [headercolor: #f07178] {
muter_id uuid [not null, ref: > users.id]
muted_id uuid [not null, ref: > users.id]
mute_posts boolean [not null, default: true]
mute_stories boolean [not null, default: true]
created_at timestamptz [not null, default: `now()`]
indexes {
(muter_id, muted_id) [pk]
}
Note: 'A quiet unfollow: the follow stays, the content stops appearing, and the muted person is never told.'
}
Table reports [headercolor: #f07178] {
id uuid [pk]
reporter_id uuid [not null, ref: > users.id]
target_kind report_target [not null]
reported_user_id uuid [not null, ref: > users.id, note: 'Always set: the author of the reported content, or the account itself']
post_id uuid [ref: > posts.id]
comment_id uuid [ref: > comments.id]
story_id uuid [ref: > stories.id]
reason report_reason [not null]
details text
status report_status [not null, default: 'open']
created_at timestamptz [not null, default: `now()`]
resolved_at timestamptz
indexes {
(status, created_at) [note: 'The moderation queue, oldest first']
reported_user_id
}
Note: 'User reports. Many reports about one post are many rows; the queue groups them by target so a moderator reviews it once.'
}
Table moderation_actions [headercolor: #f07178] {
id uuid [pk]
report_id uuid [ref: > reports.id, note: 'Null for proactive actions, e.g. from an automated classifier']
moderator_id uuid [ref: > users.id, note: 'A staff user; null when an automated system acted']
action moderation_action_kind [not null]
target_user_id uuid [not null, ref: > users.id]
post_id uuid [ref: > posts.id]
comment_id uuid [ref: > comments.id]
story_id uuid [ref: > stories.id]
policy_code varchar(50) [not null, note: 'Which rule was broken, e.g. HARASSMENT_2. Quoted in the notice the user receives']
reason text
expires_at timestamptz [note: 'For time-limited suspensions']
reverses_action_id uuid [ref: > moderation_actions.id, note: 'A successful appeal is a new restore action pointing at the one it undoes']
created_at timestamptz [not null, default: `now()`]
Note: 'Append-only log of everything moderation did. A moderator never hard-deletes content: posts, comments and stories point back here through removed_by_action_id.'
}
// ---------------------------------------------------------------------------
// ENUMS
// ---------------------------------------------------------------------------
Enum account_status {
active
deactivated
suspended
banned
deleted
}
Enum follow_state {
pending
accepted
}
Enum post_status {
processing [note: 'Media still transcoding; visible only to the author']
published
archived [note: 'Hidden from the profile by the author, restorable']
}
Enum media_type {
photo
video
}
Enum processing_status {
pending
processing
ready
failed
}
Enum media_variant {
thumbnail_150
small_320
medium_640
large_1080
video_480p
video_720p
video_1080p
}
Enum story_audience {
followers
close_friends
}
Enum notification_kind {
like
comment
reply
comment_like
mention
photo_tag
follow
follow_request
follow_accepted
moderation_notice
}
Enum device_platform {
ios
android
web
}
Enum report_target {
post
comment
story
account
}
Enum report_reason {
spam
harassment
hate_speech
nudity
violence
self_harm
impersonation
scam
intellectual_property
other
}
Enum report_status {
open
in_review
actioned
dismissed
}
Enum moderation_action_kind {
remove_content
restore_content
warn_user
suspend_user
ban_user
reinstate_user
}Photo Sharing database schema as SQL
The same schema as CREATE TABLE statements, exported by DB Planner.
PostgreSQL is shown first; MySQL and SQL Server are below it.
PostgreSQL
CREATE EXTENSION IF NOT EXISTS "citext";
CREATE TYPE "account_status" AS ENUM (
'active',
'deactivated',
'suspended',
'banned',
'deleted'
);
CREATE TYPE "follow_state" AS ENUM (
'pending',
'accepted'
);
CREATE TYPE "post_status" AS ENUM (
'processing',
'published',
'archived'
);
CREATE TYPE "media_type" AS ENUM (
'photo',
'video'
);
CREATE TYPE "processing_status" AS ENUM (
'pending',
'processing',
'ready',
'failed'
);
CREATE TYPE "media_variant" AS ENUM (
'thumbnail_150',
'small_320',
'medium_640',
'large_1080',
'video_480p',
'video_720p',
'video_1080p'
);
CREATE TYPE "story_audience" AS ENUM (
'followers',
'close_friends'
);
CREATE TYPE "notification_kind" AS ENUM (
'like',
'comment',
'reply',
'comment_like',
'mention',
'photo_tag',
'follow',
'follow_request',
'follow_accepted',
'moderation_notice'
);
CREATE TYPE "device_platform" AS ENUM (
'ios',
'android',
'web'
);
CREATE TYPE "report_target" AS ENUM (
'post',
'comment',
'story',
'account'
);
CREATE TYPE "report_reason" AS ENUM (
'spam',
'harassment',
'hate_speech',
'nudity',
'violence',
'self_harm',
'impersonation',
'scam',
'intellectual_property',
'other'
);
CREATE TYPE "report_status" AS ENUM (
'open',
'in_review',
'actioned',
'dismissed'
);
CREATE TYPE "moderation_action_kind" AS ENUM (
'remove_content',
'restore_content',
'warn_user',
'suspend_user',
'ban_user',
'reinstate_user'
);
CREATE TABLE "users" (
"id" uuid PRIMARY KEY,
"email" varchar(254) UNIQUE,
"phone" varchar(30) UNIQUE,
"password_hash" varchar(200),
"status" account_status NOT NULL DEFAULT 'active',
"is_staff" boolean NOT NULL DEFAULT false,
"date_of_birth" date,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"deactivated_at" timestamptz,
"deleted_at" timestamptz
);
CREATE TABLE "profiles" (
"user_id" uuid PRIMARY KEY,
"username" varchar(30) UNIQUE NOT NULL,
"display_name" varchar(64),
"bio" varchar(150),
"website_url" varchar(300),
"avatar_storage_key" varchar(300),
"is_private" boolean NOT NULL DEFAULT false,
"follower_count" integer NOT NULL DEFAULT 0,
"following_count" integer NOT NULL DEFAULT 0,
"post_count" integer NOT NULL DEFAULT 0,
"updated_at" timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE "follows" (
"follower_id" uuid NOT NULL,
"followee_id" uuid NOT NULL,
"state" follow_state NOT NULL,
"requested_at" timestamptz NOT NULL DEFAULT (now()),
"accepted_at" timestamptz,
PRIMARY KEY ("follower_id", "followee_id")
);
CREATE TABLE "close_friends" (
"owner_id" uuid NOT NULL,
"friend_id" uuid NOT NULL,
"added_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("owner_id", "friend_id")
);
CREATE TABLE "places" (
"id" uuid PRIMARY KEY,
"name" varchar(200) NOT NULL,
"latitude" numeric(9,6) NOT NULL,
"longitude" numeric(9,6) NOT NULL,
"city" varchar(120),
"country" char(2),
"provider_place_id" varchar(100) UNIQUE
);
CREATE TABLE "posts" (
"id" uuid PRIMARY KEY,
"author_id" uuid NOT NULL,
"caption" text,
"place_id" uuid,
"status" post_status NOT NULL DEFAULT 'processing',
"comments_disabled" boolean NOT NULL DEFAULT false,
"hide_like_count" boolean NOT NULL DEFAULT false,
"like_count" integer NOT NULL DEFAULT 0,
"comment_count" integer NOT NULL DEFAULT 0,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"edited_at" timestamptz,
"deleted_at" timestamptz,
"removed_by_action_id" uuid
);
CREATE TABLE "post_media" (
"id" uuid PRIMARY KEY,
"post_id" uuid NOT NULL,
"position" smallint NOT NULL,
"media_type" media_type NOT NULL,
"original_storage_key" varchar(300) NOT NULL,
"width" integer NOT NULL,
"height" integer NOT NULL,
"duration_ms" integer,
"alt_text" varchar(500),
"blurhash" varchar(60),
"processing_status" processing_status NOT NULL DEFAULT 'pending'
);
CREATE TABLE "media_variants" (
"id" uuid PRIMARY KEY,
"post_media_id" uuid NOT NULL,
"variant" media_variant NOT NULL,
"content_type" varchar(50) NOT NULL,
"storage_key" varchar(300) UNIQUE NOT NULL,
"width" integer NOT NULL,
"height" integer NOT NULL,
"bytes" integer NOT NULL
);
CREATE TABLE "photo_tags" (
"post_media_id" uuid NOT NULL,
"tagged_user_id" uuid NOT NULL,
"x" numeric(5,4) NOT NULL,
"y" numeric(5,4) NOT NULL,
"approved" boolean NOT NULL DEFAULT true,
"created_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("post_media_id", "tagged_user_id")
);
CREATE TABLE "hashtags" (
"id" uuid PRIMARY KEY,
"name" varchar(100) UNIQUE NOT NULL,
"post_count" integer NOT NULL DEFAULT 0,
"is_blocked" boolean NOT NULL DEFAULT false,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE "post_hashtags" (
"post_id" uuid NOT NULL,
"hashtag_id" uuid NOT NULL,
"created_at" timestamptz NOT NULL,
PRIMARY KEY ("post_id", "hashtag_id")
);
CREATE TABLE "mentions" (
"id" uuid PRIMARY KEY,
"mentioned_user_id" uuid NOT NULL,
"post_id" uuid,
"comment_id" uuid,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE "post_likes" (
"post_id" uuid NOT NULL,
"user_id" uuid NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("post_id", "user_id")
);
CREATE TABLE "comments" (
"id" uuid PRIMARY KEY,
"post_id" uuid NOT NULL,
"author_id" uuid NOT NULL,
"parent_comment_id" uuid,
"body" varchar(2200) NOT NULL,
"like_count" integer NOT NULL DEFAULT 0,
"reply_count" integer NOT NULL DEFAULT 0,
"is_pinned" boolean NOT NULL DEFAULT false,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"edited_at" timestamptz,
"deleted_at" timestamptz,
"removed_by_action_id" uuid
);
CREATE TABLE "comment_likes" (
"comment_id" uuid NOT NULL,
"user_id" uuid NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("comment_id", "user_id")
);
CREATE TABLE "saves" (
"user_id" uuid NOT NULL,
"post_id" uuid NOT NULL,
"saved_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("user_id", "post_id")
);
CREATE TABLE "collections" (
"id" uuid PRIMARY KEY,
"owner_id" uuid NOT NULL,
"name" varchar(100) NOT NULL,
"cover_post_id" uuid,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE "collection_posts" (
"collection_id" uuid NOT NULL,
"user_id" uuid NOT NULL,
"post_id" uuid NOT NULL,
"added_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("collection_id", "post_id")
);
CREATE TABLE "stories" (
"id" uuid PRIMARY KEY,
"author_id" uuid NOT NULL,
"media_type" media_type NOT NULL,
"storage_key" varchar(300) NOT NULL,
"width" integer NOT NULL,
"height" integer NOT NULL,
"duration_ms" integer NOT NULL DEFAULT 5000,
"audience" story_audience NOT NULL DEFAULT 'followers',
"view_count" integer NOT NULL DEFAULT 0,
"created_at" timestamptz NOT NULL DEFAULT (now()),
"expires_at" timestamptz NOT NULL,
"deleted_at" timestamptz,
"removed_by_action_id" uuid
);
CREATE TABLE "story_views" (
"story_id" uuid NOT NULL,
"viewer_id" uuid NOT NULL,
"viewed_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("story_id", "viewer_id")
);
CREATE TABLE "highlights" (
"id" uuid PRIMARY KEY,
"owner_id" uuid NOT NULL,
"title" varchar(30) NOT NULL,
"cover_storage_key" varchar(300),
"position" smallint NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE "highlight_stories" (
"highlight_id" uuid NOT NULL,
"story_id" uuid NOT NULL,
"position" smallint NOT NULL,
PRIMARY KEY ("highlight_id", "story_id")
);
CREATE TABLE "notifications" (
"id" uuid PRIMARY KEY,
"recipient_id" uuid NOT NULL,
"actor_id" uuid,
"kind" notification_kind NOT NULL,
"post_id" uuid,
"comment_id" uuid,
"story_id" uuid,
"group_key" varchar(120),
"created_at" timestamptz NOT NULL DEFAULT (now()),
"read_at" timestamptz
);
CREATE TABLE "push_devices" (
"id" uuid PRIMARY KEY,
"user_id" uuid NOT NULL,
"platform" device_platform NOT NULL,
"push_token" varchar(500) UNIQUE NOT NULL,
"app_version" varchar(20),
"last_seen_at" timestamptz NOT NULL,
"disabled_at" timestamptz
);
CREATE TABLE "blocks" (
"blocker_id" uuid NOT NULL,
"blocked_id" uuid NOT NULL,
"created_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("blocker_id", "blocked_id")
);
CREATE TABLE "mutes" (
"muter_id" uuid NOT NULL,
"muted_id" uuid NOT NULL,
"mute_posts" boolean NOT NULL DEFAULT true,
"mute_stories" boolean NOT NULL DEFAULT true,
"created_at" timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ("muter_id", "muted_id")
);
CREATE TABLE "reports" (
"id" uuid PRIMARY KEY,
"reporter_id" uuid NOT NULL,
"target_kind" report_target NOT NULL,
"reported_user_id" uuid NOT NULL,
"post_id" uuid,
"comment_id" uuid,
"story_id" uuid,
"reason" report_reason NOT NULL,
"details" text,
"status" report_status NOT NULL DEFAULT 'open',
"created_at" timestamptz NOT NULL DEFAULT (now()),
"resolved_at" timestamptz
);
CREATE TABLE "moderation_actions" (
"id" uuid PRIMARY KEY,
"report_id" uuid,
"moderator_id" uuid,
"action" moderation_action_kind NOT NULL,
"target_user_id" uuid NOT NULL,
"post_id" uuid,
"comment_id" uuid,
"story_id" uuid,
"policy_code" varchar(50) NOT NULL,
"reason" text,
"expires_at" timestamptz,
"reverses_action_id" uuid,
"created_at" timestamptz NOT NULL DEFAULT (now())
);
CREATE INDEX ON "follows" ("followee_id", "state");
CREATE INDEX ON "posts" ("author_id", "created_at");
CREATE UNIQUE INDEX ON "post_media" ("post_id", "position");
CREATE UNIQUE INDEX ON "media_variants" ("post_media_id", "variant", "content_type");
CREATE INDEX ON "photo_tags" ("tagged_user_id");
CREATE INDEX ON "post_hashtags" ("hashtag_id", "created_at");
CREATE INDEX ON "mentions" ("mentioned_user_id");
CREATE INDEX ON "post_likes" ("user_id", "created_at");
CREATE INDEX ON "comments" ("post_id", "parent_comment_id", "created_at");
CREATE INDEX ON "saves" ("user_id", "saved_at");
CREATE UNIQUE INDEX ON "collections" ("owner_id", "name");
CREATE INDEX ON "stories" ("author_id", "expires_at");
CREATE INDEX ON "notifications" ("recipient_id", "created_at");
CREATE INDEX ON "notifications" ("recipient_id", "group_key");
CREATE INDEX ON "blocks" ("blocked_id");
CREATE INDEX ON "reports" ("status", "created_at");
CREATE INDEX ON "reports" ("reported_user_id");
COMMENT ON TABLE "users" IS 'Login identity and account state. Everything public-facing lives on profiles, so credentials are never part of a query that renders a page.';
COMMENT ON COLUMN "users"."phone" IS 'E.164 format. An account needs an email or a phone (a CHECK constraint), not both';
COMMENT ON COLUMN "users"."password_hash" IS 'Null for accounts that sign in only with a social login or a one-time code';
COMMENT ON COLUMN "users"."is_staff" IS 'Can work the moderation queue. A role on the same account, never a separate login';
COMMENT ON COLUMN "users"."date_of_birth" IS 'Minimum age checks and teen account defaults';
COMMENT ON COLUMN "users"."deactivated_at" IS 'User-initiated pause: content hidden, fully restorable';
COMMENT ON COLUMN "users"."deleted_at" IS 'Soft delete. A purge job erases personal data after the grace period; the row then keeps only its id';
COMMENT ON TABLE "profiles" IS 'The public face of an account, one to one with users. The counters are denormalized for profile headers and reconciled from their source rows by a background job.';
COMMENT ON COLUMN "profiles"."username" IS 'Stored lowercase; uniqueness is case-insensitive (citext, or a unique index on lower(username))';
COMMENT ON COLUMN "profiles"."avatar_storage_key" IS 'Object storage key. Avatar sizes are derived by naming convention';
COMMENT ON COLUMN "profiles"."is_private" IS 'Private accounts turn every new follow into a request';
COMMENT ON COLUMN "profiles"."follower_count" IS 'CACHE. Authoritative: accepted rows in follows';
COMMENT ON COLUMN "profiles"."following_count" IS 'CACHE. Authoritative: accepted rows in follows';
COMMENT ON COLUMN "profiles"."post_count" IS 'CACHE. Authoritative: live rows in posts';
COMMENT ON TABLE "follows" IS 'One row per directed follow. A request to a private account is the same row in state pending, so approving is an UPDATE and every visibility check reads only state = accepted. Declining deletes the row.';
COMMENT ON COLUMN "follows"."state" IS 'pending until a private account approves; accepted straight away otherwise';
COMMENT ON TABLE "close_friends" IS 'A private audience list for stories. The friend is never told they were added or removed.';
COMMENT ON TABLE "places" IS 'Locations a post can be tagged at. A place page lists posts by place_id, so coordinates are stored once.';
COMMENT ON COLUMN "places"."provider_place_id" IS 'Id from the maps provider the location was picked from, so re-picking it finds this row';
COMMENT ON TABLE "posts" IS 'A post is a container: caption, place and settings. The photos and videos are post_media rows, which is what makes a carousel simply a post with more than one media row.';
COMMENT ON COLUMN "posts"."caption" IS 'Raw text as typed. Hashtags and mentions are parsed out of it on every save';
COMMENT ON COLUMN "posts"."like_count" IS 'CACHE of count(post_likes)';
COMMENT ON COLUMN "posts"."comment_count" IS 'CACHE of live comments';
COMMENT ON COLUMN "posts"."deleted_at" IS 'Soft delete by the author';
COMMENT ON COLUMN "posts"."removed_by_action_id" IS 'Set when moderation removes the post. Clearing it restores the post exactly as it was';
COMMENT ON TABLE "post_media" IS 'One photo or video in a post, in carousel order. Bytes live in object storage; the database holds keys and metadata only.';
COMMENT ON COLUMN "post_media"."position" IS '0-based order in the carousel';
COMMENT ON COLUMN "post_media"."original_storage_key" IS 'The upload as received. Kept private; nothing serves it directly';
COMMENT ON COLUMN "post_media"."duration_ms" IS 'Videos only';
COMMENT ON COLUMN "post_media"."alt_text" IS 'Written by the author or generated; read aloud by screen readers';
COMMENT ON COLUMN "post_media"."blurhash" IS 'Tiny placeholder string the app paints while the real image loads';
COMMENT ON TABLE "media_variants" IS 'Resized and transcoded renditions written by the media pipeline. A feed asks for the variant that fits the screen, so a phone on a slow network never downloads the original.';
COMMENT ON COLUMN "media_variants"."content_type" IS 'e.g. image/webp, image/jpeg, video/mp4';
COMMENT ON TABLE "photo_tags" IS 'People tagged ON a photo, at a point. Different from a mention, which lives in text.';
COMMENT ON COLUMN "photo_tags"."x" IS '0 to 1, a fraction of the width, so the tag lands on the same spot in every variant';
COMMENT ON COLUMN "photo_tags"."y" IS '0 to 1, a fraction of the height';
COMMENT ON COLUMN "photo_tags"."approved" IS 'False while waiting, for users who review tags manually';
COMMENT ON TABLE "hashtags" IS 'One row per distinct tag ever used.';
COMMENT ON COLUMN "hashtags"."name" IS 'Lowercased, without the #. Display casing comes from the caption itself';
COMMENT ON COLUMN "hashtags"."post_count" IS 'CACHE of post_hashtags rows';
COMMENT ON COLUMN "hashtags"."is_blocked" IS 'Blocked tags are not searchable and their page shows nothing';
COMMENT ON TABLE "post_hashtags" IS 'Derived from the caption on every save: the caption is the source, these rows are its index. Editing a caption rewrites the set.';
COMMENT ON COLUMN "post_hashtags"."created_at" IS 'Copied from the post, so a hashtag page sorts by time without joining posts';
COMMENT ON TABLE "mentions" IS '@username references parsed from a caption or a comment. Exactly one of post_id and comment_id is set (a CHECK constraint). Stored by user id, so a username change never breaks the link.';
COMMENT ON TABLE "post_likes" IS 'The authoritative likes. posts.like_count is a cache incremented in the same transaction and reconciled from these rows; when the two disagree, these rows win.';
COMMENT ON TABLE "comments" IS 'Threaded comments. A soft-deleted parent keeps its replies attached instead of orphaning them.';
COMMENT ON COLUMN "comments"."parent_comment_id" IS 'Null for a top-level comment. Replies are one level deep: replying to a reply points at the same top-level comment and mentions the person';
COMMENT ON COLUMN "comments"."like_count" IS 'CACHE of comment_likes';
COMMENT ON COLUMN "comments"."reply_count" IS 'CACHE of live replies';
COMMENT ON COLUMN "comments"."is_pinned" IS 'The post author can pin a few comments to the top';
COMMENT ON COLUMN "comments"."deleted_at" IS 'Soft delete. Replies stay attached and the parent renders as deleted';
COMMENT ON TABLE "comment_likes" IS 'Likes on comments. comments.like_count is their cache.';
COMMENT ON TABLE "saves" IS 'Bookmarks. Private to the saver; the author only ever sees an aggregate count.';
COMMENT ON TABLE "collections" IS 'Optional named folders over saved posts.';
COMMENT ON COLUMN "collections"."cover_post_id" IS 'Null means use the most recently added post';
COMMENT ON TABLE "collection_posts" IS 'Which saved posts are filed in which collection. The composite foreign key to saves cascades, so unsaving a post removes it from every collection at once.';
COMMENT ON COLUMN "collection_posts"."user_id" IS 'The collection owner, repeated so the row can reference the save it files';
COMMENT ON TABLE "stories" IS 'Ephemeral posts. An expired story leaves the tray but stays in the author archive, which is how it can still be added to a highlight.';
COMMENT ON COLUMN "stories"."duration_ms" IS 'How long it shows. Videos use their own length, up to a cap';
COMMENT ON COLUMN "stories"."view_count" IS 'CACHE of story_views';
COMMENT ON COLUMN "stories"."expires_at" IS 'created_at + 24 hours. Feeds filter on it; no job deletes anything at expiry';
COMMENT ON TABLE "story_views" IS 'Who saw a story, visible only to its author. Purged a couple of days after expiry; the archive keeps stories.view_count.';
COMMENT ON TABLE "highlights" IS 'Named sets of past stories pinned to a profile. They outlive expiry because they point at story rows that were never deleted.';
COMMENT ON COLUMN "highlights"."position" IS 'Order on the profile';
COMMENT ON TABLE "highlight_stories" IS 'Which stories are in a highlight, in order.';
COMMENT ON TABLE "notifications" IS 'The in-app activity feed. One row per event, grouped at read time, so an undone like deletes exactly its own row.';
COMMENT ON COLUMN "notifications"."actor_id" IS 'Who did it; null for system notices';
COMMENT ON COLUMN "notifications"."group_key" IS 'e.g. like:post:<id>. The inbox collapses rows sharing a key into "sam and 41 others liked your post"';
COMMENT ON TABLE "push_devices" IS 'Where to deliver push notifications. One user, many phones and tablets.';
COMMENT ON COLUMN "push_devices"."push_token" IS 'Issued by the OS push service. It rotates, so the app upserts it on every launch';
COMMENT ON COLUMN "push_devices"."disabled_at" IS 'Set when the push service reports the token invalid';
COMMENT ON TABLE "blocks" IS 'A block removes any follow in both directions and hides each person from the other everywhere: feed, search, comments, stories. Every read path filters on it.';
COMMENT ON TABLE "mutes" IS 'A quiet unfollow: the follow stays, the content stops appearing, and the muted person is never told.';
COMMENT ON TABLE "reports" IS 'User reports. Many reports about one post are many rows; the queue groups them by target so a moderator reviews it once.';
COMMENT ON COLUMN "reports"."reported_user_id" IS 'Always set: the author of the reported content, or the account itself';
COMMENT ON TABLE "moderation_actions" IS 'Append-only log of everything moderation did. A moderator never hard-deletes content: posts, comments and stories point back here through removed_by_action_id.';
COMMENT ON COLUMN "moderation_actions"."report_id" IS 'Null for proactive actions, e.g. from an automated classifier';
COMMENT ON COLUMN "moderation_actions"."moderator_id" IS 'A staff user; null when an automated system acted';
COMMENT ON COLUMN "moderation_actions"."policy_code" IS 'Which rule was broken, e.g. HARASSMENT_2. Quoted in the notice the user receives';
COMMENT ON COLUMN "moderation_actions"."expires_at" IS 'For time-limited suspensions';
COMMENT ON COLUMN "moderation_actions"."reverses_action_id" IS 'A successful appeal is a new restore action pointing at the one it undoes';
ALTER TABLE "profiles" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "follows" ADD FOREIGN KEY ("follower_id") REFERENCES "users" ("id");
ALTER TABLE "follows" ADD FOREIGN KEY ("followee_id") REFERENCES "users" ("id");
ALTER TABLE "close_friends" ADD FOREIGN KEY ("owner_id") REFERENCES "users" ("id");
ALTER TABLE "close_friends" ADD FOREIGN KEY ("friend_id") REFERENCES "users" ("id");
ALTER TABLE "posts" ADD FOREIGN KEY ("author_id") REFERENCES "users" ("id");
ALTER TABLE "posts" ADD FOREIGN KEY ("place_id") REFERENCES "places" ("id");
ALTER TABLE "posts" ADD FOREIGN KEY ("removed_by_action_id") REFERENCES "moderation_actions" ("id");
ALTER TABLE "post_media" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "media_variants" ADD FOREIGN KEY ("post_media_id") REFERENCES "post_media" ("id");
ALTER TABLE "photo_tags" ADD FOREIGN KEY ("post_media_id") REFERENCES "post_media" ("id");
ALTER TABLE "photo_tags" ADD FOREIGN KEY ("tagged_user_id") REFERENCES "users" ("id");
ALTER TABLE "post_hashtags" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "post_hashtags" ADD FOREIGN KEY ("hashtag_id") REFERENCES "hashtags" ("id");
ALTER TABLE "mentions" ADD FOREIGN KEY ("mentioned_user_id") REFERENCES "users" ("id");
ALTER TABLE "mentions" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "mentions" ADD FOREIGN KEY ("comment_id") REFERENCES "comments" ("id");
ALTER TABLE "post_likes" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "post_likes" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "comments" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "comments" ADD FOREIGN KEY ("author_id") REFERENCES "users" ("id");
ALTER TABLE "comments" ADD FOREIGN KEY ("parent_comment_id") REFERENCES "comments" ("id");
ALTER TABLE "comments" ADD FOREIGN KEY ("removed_by_action_id") REFERENCES "moderation_actions" ("id");
ALTER TABLE "comment_likes" ADD FOREIGN KEY ("comment_id") REFERENCES "comments" ("id");
ALTER TABLE "comment_likes" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "saves" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "saves" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "collections" ADD FOREIGN KEY ("owner_id") REFERENCES "users" ("id");
ALTER TABLE "collections" ADD FOREIGN KEY ("cover_post_id") REFERENCES "posts" ("id");
ALTER TABLE "collection_posts" ADD FOREIGN KEY ("collection_id") REFERENCES "collections" ("id");
ALTER TABLE "collection_posts" ADD FOREIGN KEY ("user_id", "post_id") REFERENCES "saves" ("user_id", "post_id") ON DELETE CASCADE;
ALTER TABLE "stories" ADD FOREIGN KEY ("author_id") REFERENCES "users" ("id");
ALTER TABLE "stories" ADD FOREIGN KEY ("removed_by_action_id") REFERENCES "moderation_actions" ("id");
ALTER TABLE "story_views" ADD FOREIGN KEY ("story_id") REFERENCES "stories" ("id");
ALTER TABLE "story_views" ADD FOREIGN KEY ("viewer_id") REFERENCES "users" ("id");
ALTER TABLE "highlights" ADD FOREIGN KEY ("owner_id") REFERENCES "users" ("id");
ALTER TABLE "highlight_stories" ADD FOREIGN KEY ("highlight_id") REFERENCES "highlights" ("id");
ALTER TABLE "highlight_stories" ADD FOREIGN KEY ("story_id") REFERENCES "stories" ("id");
ALTER TABLE "notifications" ADD FOREIGN KEY ("recipient_id") REFERENCES "users" ("id");
ALTER TABLE "notifications" ADD FOREIGN KEY ("actor_id") REFERENCES "users" ("id");
ALTER TABLE "notifications" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "notifications" ADD FOREIGN KEY ("comment_id") REFERENCES "comments" ("id");
ALTER TABLE "notifications" ADD FOREIGN KEY ("story_id") REFERENCES "stories" ("id");
ALTER TABLE "push_devices" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");
ALTER TABLE "blocks" ADD FOREIGN KEY ("blocker_id") REFERENCES "users" ("id");
ALTER TABLE "blocks" ADD FOREIGN KEY ("blocked_id") REFERENCES "users" ("id");
ALTER TABLE "mutes" ADD FOREIGN KEY ("muter_id") REFERENCES "users" ("id");
ALTER TABLE "mutes" ADD FOREIGN KEY ("muted_id") REFERENCES "users" ("id");
ALTER TABLE "reports" ADD FOREIGN KEY ("reporter_id") REFERENCES "users" ("id");
ALTER TABLE "reports" ADD FOREIGN KEY ("reported_user_id") REFERENCES "users" ("id");
ALTER TABLE "reports" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "reports" ADD FOREIGN KEY ("comment_id") REFERENCES "comments" ("id");
ALTER TABLE "reports" ADD FOREIGN KEY ("story_id") REFERENCES "stories" ("id");
ALTER TABLE "moderation_actions" ADD FOREIGN KEY ("report_id") REFERENCES "reports" ("id");
ALTER TABLE "moderation_actions" ADD FOREIGN KEY ("moderator_id") REFERENCES "users" ("id");
ALTER TABLE "moderation_actions" ADD FOREIGN KEY ("target_user_id") REFERENCES "users" ("id");
ALTER TABLE "moderation_actions" ADD FOREIGN KEY ("post_id") REFERENCES "posts" ("id");
ALTER TABLE "moderation_actions" ADD FOREIGN KEY ("comment_id") REFERENCES "comments" ("id");
ALTER TABLE "moderation_actions" ADD FOREIGN KEY ("story_id") REFERENCES "stories" ("id");
ALTER TABLE "moderation_actions" ADD FOREIGN KEY ("reverses_action_id") REFERENCES "moderation_actions" ("id");MySQL
CREATE TABLE `users` (
`id` uuid PRIMARY KEY,
`email` varchar(254) UNIQUE,
`phone` varchar(30) UNIQUE COMMENT 'E.164 format. An account needs an email or a phone (a CHECK constraint), not both',
`password_hash` varchar(200) COMMENT 'Null for accounts that sign in only with a social login or a one-time code',
`status` ENUM ('active', 'deactivated', 'suspended', 'banned', 'deleted') NOT NULL DEFAULT 'active',
`is_staff` boolean NOT NULL DEFAULT false COMMENT 'Can work the moderation queue. A role on the same account, never a separate login',
`date_of_birth` date COMMENT 'Minimum age checks and teen account defaults',
`created_at` timestamptz NOT NULL DEFAULT (now()),
`deactivated_at` timestamptz COMMENT 'User-initiated pause: content hidden, fully restorable',
`deleted_at` timestamptz COMMENT 'Soft delete. A purge job erases personal data after the grace period; the row then keeps only its id'
);
CREATE TABLE `profiles` (
`user_id` uuid PRIMARY KEY,
`username` varchar(30) UNIQUE NOT NULL COMMENT 'Stored lowercase; uniqueness is case-insensitive (citext, or a unique index on lower(username))',
`display_name` varchar(64),
`bio` varchar(150),
`website_url` varchar(300),
`avatar_storage_key` varchar(300) COMMENT 'Object storage key. Avatar sizes are derived by naming convention',
`is_private` boolean NOT NULL DEFAULT false COMMENT 'Private accounts turn every new follow into a request',
`follower_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE. Authoritative: accepted rows in follows',
`following_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE. Authoritative: accepted rows in follows',
`post_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE. Authoritative: live rows in posts',
`updated_at` timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE `follows` (
`follower_id` uuid NOT NULL,
`followee_id` uuid NOT NULL,
`state` ENUM ('pending', 'accepted') NOT NULL COMMENT 'pending until a private account approves; accepted straight away otherwise',
`requested_at` timestamptz NOT NULL DEFAULT (now()),
`accepted_at` timestamptz,
PRIMARY KEY (`follower_id`, `followee_id`)
);
CREATE TABLE `close_friends` (
`owner_id` uuid NOT NULL,
`friend_id` uuid NOT NULL,
`added_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`owner_id`, `friend_id`)
);
CREATE TABLE `places` (
`id` uuid PRIMARY KEY,
`name` varchar(200) NOT NULL,
`latitude` numeric(9,6) NOT NULL,
`longitude` numeric(9,6) NOT NULL,
`city` varchar(120),
`country` char(2),
`provider_place_id` varchar(100) UNIQUE COMMENT 'Id from the maps provider the location was picked from, so re-picking it finds this row'
);
CREATE TABLE `posts` (
`id` uuid PRIMARY KEY,
`author_id` uuid NOT NULL,
`caption` text COMMENT 'Raw text as typed. Hashtags and mentions are parsed out of it on every save',
`place_id` uuid,
`status` ENUM ('processing', 'published', 'archived') NOT NULL DEFAULT 'processing',
`comments_disabled` boolean NOT NULL DEFAULT false,
`hide_like_count` boolean NOT NULL DEFAULT false,
`like_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE of count(post_likes)',
`comment_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE of live comments',
`created_at` timestamptz NOT NULL DEFAULT (now()),
`edited_at` timestamptz,
`deleted_at` timestamptz COMMENT 'Soft delete by the author',
`removed_by_action_id` uuid COMMENT 'Set when moderation removes the post. Clearing it restores the post exactly as it was'
);
CREATE TABLE `post_media` (
`id` uuid PRIMARY KEY,
`post_id` uuid NOT NULL,
`position` smallint NOT NULL COMMENT '0-based order in the carousel',
`media_type` ENUM ('photo', 'video') NOT NULL,
`original_storage_key` varchar(300) NOT NULL COMMENT 'The upload as received. Kept private; nothing serves it directly',
`width` integer NOT NULL,
`height` integer NOT NULL,
`duration_ms` integer COMMENT 'Videos only',
`alt_text` varchar(500) COMMENT 'Written by the author or generated; read aloud by screen readers',
`blurhash` varchar(60) COMMENT 'Tiny placeholder string the app paints while the real image loads',
`processing_status` ENUM ('pending', 'processing', 'ready', 'failed') NOT NULL DEFAULT 'pending'
);
CREATE TABLE `media_variants` (
`id` uuid PRIMARY KEY,
`post_media_id` uuid NOT NULL,
`variant` ENUM ('thumbnail_150', 'small_320', 'medium_640', 'large_1080', 'video_480p', 'video_720p', 'video_1080p') NOT NULL,
`content_type` varchar(50) NOT NULL COMMENT 'e.g. image/webp, image/jpeg, video/mp4',
`storage_key` varchar(300) UNIQUE NOT NULL,
`width` integer NOT NULL,
`height` integer NOT NULL,
`bytes` integer NOT NULL
);
CREATE TABLE `photo_tags` (
`post_media_id` uuid NOT NULL,
`tagged_user_id` uuid NOT NULL,
`x` numeric(5,4) NOT NULL COMMENT '0 to 1, a fraction of the width, so the tag lands on the same spot in every variant',
`y` numeric(5,4) NOT NULL COMMENT '0 to 1, a fraction of the height',
`approved` boolean NOT NULL DEFAULT true COMMENT 'False while waiting, for users who review tags manually',
`created_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`post_media_id`, `tagged_user_id`)
);
CREATE TABLE `hashtags` (
`id` uuid PRIMARY KEY,
`name` varchar(100) UNIQUE NOT NULL COMMENT 'Lowercased, without the #. Display casing comes from the caption itself',
`post_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE of post_hashtags rows',
`is_blocked` boolean NOT NULL DEFAULT false COMMENT 'Blocked tags are not searchable and their page shows nothing',
`created_at` timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE `post_hashtags` (
`post_id` uuid NOT NULL,
`hashtag_id` uuid NOT NULL,
`created_at` timestamptz NOT NULL COMMENT 'Copied from the post, so a hashtag page sorts by time without joining posts',
PRIMARY KEY (`post_id`, `hashtag_id`)
);
CREATE TABLE `mentions` (
`id` uuid PRIMARY KEY,
`mentioned_user_id` uuid NOT NULL,
`post_id` uuid,
`comment_id` uuid,
`created_at` timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE `post_likes` (
`post_id` uuid NOT NULL,
`user_id` uuid NOT NULL,
`created_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`post_id`, `user_id`)
);
CREATE TABLE `comments` (
`id` uuid PRIMARY KEY,
`post_id` uuid NOT NULL,
`author_id` uuid NOT NULL,
`parent_comment_id` uuid COMMENT 'Null for a top-level comment. Replies are one level deep: replying to a reply points at the same top-level comment and mentions the person',
`body` varchar(2200) NOT NULL,
`like_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE of comment_likes',
`reply_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE of live replies',
`is_pinned` boolean NOT NULL DEFAULT false COMMENT 'The post author can pin a few comments to the top',
`created_at` timestamptz NOT NULL DEFAULT (now()),
`edited_at` timestamptz,
`deleted_at` timestamptz COMMENT 'Soft delete. Replies stay attached and the parent renders as deleted',
`removed_by_action_id` uuid
);
CREATE TABLE `comment_likes` (
`comment_id` uuid NOT NULL,
`user_id` uuid NOT NULL,
`created_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`comment_id`, `user_id`)
);
CREATE TABLE `saves` (
`user_id` uuid NOT NULL,
`post_id` uuid NOT NULL,
`saved_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`user_id`, `post_id`)
);
CREATE TABLE `collections` (
`id` uuid PRIMARY KEY,
`owner_id` uuid NOT NULL,
`name` varchar(100) NOT NULL,
`cover_post_id` uuid COMMENT 'Null means use the most recently added post',
`created_at` timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE `collection_posts` (
`collection_id` uuid NOT NULL,
`user_id` uuid NOT NULL COMMENT 'The collection owner, repeated so the row can reference the save it files',
`post_id` uuid NOT NULL,
`added_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`collection_id`, `post_id`)
);
CREATE TABLE `stories` (
`id` uuid PRIMARY KEY,
`author_id` uuid NOT NULL,
`media_type` ENUM ('photo', 'video') NOT NULL,
`storage_key` varchar(300) NOT NULL,
`width` integer NOT NULL,
`height` integer NOT NULL,
`duration_ms` integer NOT NULL DEFAULT 5000 COMMENT 'How long it shows. Videos use their own length, up to a cap',
`audience` ENUM ('followers', 'close_friends') NOT NULL DEFAULT 'followers',
`view_count` integer NOT NULL DEFAULT 0 COMMENT 'CACHE of story_views',
`created_at` timestamptz NOT NULL DEFAULT (now()),
`expires_at` timestamptz NOT NULL COMMENT 'created_at + 24 hours. Feeds filter on it; no job deletes anything at expiry',
`deleted_at` timestamptz,
`removed_by_action_id` uuid
);
CREATE TABLE `story_views` (
`story_id` uuid NOT NULL,
`viewer_id` uuid NOT NULL,
`viewed_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`story_id`, `viewer_id`)
);
CREATE TABLE `highlights` (
`id` uuid PRIMARY KEY,
`owner_id` uuid NOT NULL,
`title` varchar(30) NOT NULL,
`cover_storage_key` varchar(300),
`position` smallint NOT NULL COMMENT 'Order on the profile',
`created_at` timestamptz NOT NULL DEFAULT (now())
);
CREATE TABLE `highlight_stories` (
`highlight_id` uuid NOT NULL,
`story_id` uuid NOT NULL,
`position` smallint NOT NULL,
PRIMARY KEY (`highlight_id`, `story_id`)
);
CREATE TABLE `notifications` (
`id` uuid PRIMARY KEY,
`recipient_id` uuid NOT NULL,
`actor_id` uuid COMMENT 'Who did it; null for system notices',
`kind` ENUM ('like', 'comment', 'reply', 'comment_like', 'mention', 'photo_tag', 'follow', 'follow_request', 'follow_accepted', 'moderation_notice') NOT NULL,
`post_id` uuid,
`comment_id` uuid,
`story_id` uuid,
`group_key` varchar(120) COMMENT 'e.g. like:post:<id>. The inbox collapses rows sharing a key into "sam and 41 others liked your post"',
`created_at` timestamptz NOT NULL DEFAULT (now()),
`read_at` timestamptz
);
CREATE TABLE `push_devices` (
`id` uuid PRIMARY KEY,
`user_id` uuid NOT NULL,
`platform` ENUM ('ios', 'android', 'web') NOT NULL,
`push_token` varchar(500) UNIQUE NOT NULL COMMENT 'Issued by the OS push service. It rotates, so the app upserts it on every launch',
`app_version` varchar(20),
`last_seen_at` timestamptz NOT NULL,
`disabled_at` timestamptz COMMENT 'Set when the push service reports the token invalid'
);
CREATE TABLE `blocks` (
`blocker_id` uuid NOT NULL,
`blocked_id` uuid NOT NULL,
`created_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`blocker_id`, `blocked_id`)
);
CREATE TABLE `mutes` (
`muter_id` uuid NOT NULL,
`muted_id` uuid NOT NULL,
`mute_posts` boolean NOT NULL DEFAULT true,
`mute_stories` boolean NOT NULL DEFAULT true,
`created_at` timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY (`muter_id`, `muted_id`)
);
CREATE TABLE `reports` (
`id` uuid PRIMARY KEY,
`reporter_id` uuid NOT NULL,
`target_kind` ENUM ('post', 'comment', 'story', 'account') NOT NULL,
`reported_user_id` uuid NOT NULL COMMENT 'Always set: the author of the reported content, or the account itself',
`post_id` uuid,
`comment_id` uuid,
`story_id` uuid,
`reason` ENUM ('spam', 'harassment', 'hate_speech', 'nudity', 'violence', 'self_harm', 'impersonation', 'scam', 'intellectual_property', 'other') NOT NULL,
`details` text,
`status` ENUM ('open', 'in_review', 'actioned', 'dismissed') NOT NULL DEFAULT 'open',
`created_at` timestamptz NOT NULL DEFAULT (now()),
`resolved_at` timestamptz
);
CREATE TABLE `moderation_actions` (
`id` uuid PRIMARY KEY,
`report_id` uuid COMMENT 'Null for proactive actions, e.g. from an automated classifier',
`moderator_id` uuid COMMENT 'A staff user; null when an automated system acted',
`action` ENUM ('remove_content', 'restore_content', 'warn_user', 'suspend_user', 'ban_user', 'reinstate_user') NOT NULL,
`target_user_id` uuid NOT NULL,
`post_id` uuid,
`comment_id` uuid,
`story_id` uuid,
`policy_code` varchar(50) NOT NULL COMMENT 'Which rule was broken, e.g. HARASSMENT_2. Quoted in the notice the user receives',
`reason` text,
`expires_at` timestamptz COMMENT 'For time-limited suspensions',
`reverses_action_id` uuid COMMENT 'A successful appeal is a new restore action pointing at the one it undoes',
`created_at` timestamptz NOT NULL DEFAULT (now())
);
CREATE INDEX `follows_index_0` ON `follows` (`followee_id`, `state`);
CREATE INDEX `posts_index_1` ON `posts` (`author_id`, `created_at`);
CREATE UNIQUE INDEX `post_media_index_2` ON `post_media` (`post_id`, `position`);
CREATE UNIQUE INDEX `media_variants_index_3` ON `media_variants` (`post_media_id`, `variant`, `content_type`);
CREATE INDEX `photo_tags_index_4` ON `photo_tags` (`tagged_user_id`);
CREATE INDEX `post_hashtags_index_5` ON `post_hashtags` (`hashtag_id`, `created_at`);
CREATE INDEX `mentions_index_6` ON `mentions` (`mentioned_user_id`);
CREATE INDEX `post_likes_index_7` ON `post_likes` (`user_id`, `created_at`);
CREATE INDEX `comments_index_8` ON `comments` (`post_id`, `parent_comment_id`, `created_at`);
CREATE INDEX `saves_index_9` ON `saves` (`user_id`, `saved_at`);
CREATE UNIQUE INDEX `collections_index_10` ON `collections` (`owner_id`, `name`);
CREATE INDEX `stories_index_11` ON `stories` (`author_id`, `expires_at`);
CREATE INDEX `notifications_index_12` ON `notifications` (`recipient_id`, `created_at`);
CREATE INDEX `notifications_index_13` ON `notifications` (`recipient_id`, `group_key`);
CREATE INDEX `blocks_index_14` ON `blocks` (`blocked_id`);
CREATE INDEX `reports_index_15` ON `reports` (`status`, `created_at`);
CREATE INDEX `reports_index_16` ON `reports` (`reported_user_id`);
ALTER TABLE `users` COMMENT = 'Login identity and account state. Everything public-facing lives on profiles, so credentials are never part of a query that renders a page.';
ALTER TABLE `profiles` COMMENT = 'The public face of an account, one to one with users. The counters are denormalized for profile headers and reconciled from their source rows by a background job.';
ALTER TABLE `follows` COMMENT = 'One row per directed follow. A request to a private account is the same row in state pending, so approving is an UPDATE and every visibility check reads only state = accepted. Declining deletes the row.';
ALTER TABLE `close_friends` COMMENT = 'A private audience list for stories. The friend is never told they were added or removed.';
ALTER TABLE `places` COMMENT = 'Locations a post can be tagged at. A place page lists posts by place_id, so coordinates are stored once.';
ALTER TABLE `posts` COMMENT = 'A post is a container: caption, place and settings. The photos and videos are post_media rows, which is what makes a carousel simply a post with more than one media row.';
ALTER TABLE `post_media` COMMENT = 'One photo or video in a post, in carousel order. Bytes live in object storage; the database holds keys and metadata only.';
ALTER TABLE `media_variants` COMMENT = 'Resized and transcoded renditions written by the media pipeline. A feed asks for the variant that fits the screen, so a phone on a slow network never downloads the original.';
ALTER TABLE `photo_tags` COMMENT = 'People tagged ON a photo, at a point. Different from a mention, which lives in text.';
ALTER TABLE `hashtags` COMMENT = 'One row per distinct tag ever used.';
ALTER TABLE `post_hashtags` COMMENT = 'Derived from the caption on every save: the caption is the source, these rows are its index. Editing a caption rewrites the set.';
ALTER TABLE `mentions` COMMENT = '@username references parsed from a caption or a comment. Exactly one of post_id and comment_id is set (a CHECK constraint). Stored by user id, so a username change never breaks the link.';
ALTER TABLE `post_likes` COMMENT = 'The authoritative likes. posts.like_count is a cache incremented in the same transaction and reconciled from these rows; when the two disagree, these rows win.';
ALTER TABLE `comments` COMMENT = 'Threaded comments. A soft-deleted parent keeps its replies attached instead of orphaning them.';
ALTER TABLE `comment_likes` COMMENT = 'Likes on comments. comments.like_count is their cache.';
ALTER TABLE `saves` COMMENT = 'Bookmarks. Private to the saver; the author only ever sees an aggregate count.';
ALTER TABLE `collections` COMMENT = 'Optional named folders over saved posts.';
ALTER TABLE `collection_posts` COMMENT = 'Which saved posts are filed in which collection. The composite foreign key to saves cascades, so unsaving a post removes it from every collection at once.';
ALTER TABLE `stories` COMMENT = 'Ephemeral posts. An expired story leaves the tray but stays in the author archive, which is how it can still be added to a highlight.';
ALTER TABLE `story_views` COMMENT = 'Who saw a story, visible only to its author. Purged a couple of days after expiry; the archive keeps stories.view_count.';
ALTER TABLE `highlights` COMMENT = 'Named sets of past stories pinned to a profile. They outlive expiry because they point at story rows that were never deleted.';
ALTER TABLE `highlight_stories` COMMENT = 'Which stories are in a highlight, in order.';
ALTER TABLE `notifications` COMMENT = 'The in-app activity feed. One row per event, grouped at read time, so an undone like deletes exactly its own row.';
ALTER TABLE `push_devices` COMMENT = 'Where to deliver push notifications. One user, many phones and tablets.';
ALTER TABLE `blocks` COMMENT = 'A block removes any follow in both directions and hides each person from the other everywhere: feed, search, comments, stories. Every read path filters on it.';
ALTER TABLE `mutes` COMMENT = 'A quiet unfollow: the follow stays, the content stops appearing, and the muted person is never told.';
ALTER TABLE `reports` COMMENT = 'User reports. Many reports about one post are many rows; the queue groups them by target so a moderator reviews it once.';
ALTER TABLE `moderation_actions` COMMENT = 'Append-only log of everything moderation did. A moderator never hard-deletes content: posts, comments and stories point back here through removed_by_action_id.';
ALTER TABLE `profiles` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
ALTER TABLE `follows` ADD FOREIGN KEY (`follower_id`) REFERENCES `users` (`id`);
ALTER TABLE `follows` ADD FOREIGN KEY (`followee_id`) REFERENCES `users` (`id`);
ALTER TABLE `close_friends` ADD FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`);
ALTER TABLE `close_friends` ADD FOREIGN KEY (`friend_id`) REFERENCES `users` (`id`);
ALTER TABLE `posts` ADD FOREIGN KEY (`author_id`) REFERENCES `users` (`id`);
ALTER TABLE `posts` ADD FOREIGN KEY (`place_id`) REFERENCES `places` (`id`);
ALTER TABLE `posts` ADD FOREIGN KEY (`removed_by_action_id`) REFERENCES `moderation_actions` (`id`);
ALTER TABLE `post_media` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `media_variants` ADD FOREIGN KEY (`post_media_id`) REFERENCES `post_media` (`id`);
ALTER TABLE `photo_tags` ADD FOREIGN KEY (`post_media_id`) REFERENCES `post_media` (`id`);
ALTER TABLE `photo_tags` ADD FOREIGN KEY (`tagged_user_id`) REFERENCES `users` (`id`);
ALTER TABLE `post_hashtags` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `post_hashtags` ADD FOREIGN KEY (`hashtag_id`) REFERENCES `hashtags` (`id`);
ALTER TABLE `mentions` ADD FOREIGN KEY (`mentioned_user_id`) REFERENCES `users` (`id`);
ALTER TABLE `mentions` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `mentions` ADD FOREIGN KEY (`comment_id`) REFERENCES `comments` (`id`);
ALTER TABLE `post_likes` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `post_likes` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
ALTER TABLE `comments` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `comments` ADD FOREIGN KEY (`author_id`) REFERENCES `users` (`id`);
ALTER TABLE `comments` ADD FOREIGN KEY (`parent_comment_id`) REFERENCES `comments` (`id`);
ALTER TABLE `comments` ADD FOREIGN KEY (`removed_by_action_id`) REFERENCES `moderation_actions` (`id`);
ALTER TABLE `comment_likes` ADD FOREIGN KEY (`comment_id`) REFERENCES `comments` (`id`);
ALTER TABLE `comment_likes` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
ALTER TABLE `saves` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
ALTER TABLE `saves` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `collections` ADD FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`);
ALTER TABLE `collections` ADD FOREIGN KEY (`cover_post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `collection_posts` ADD FOREIGN KEY (`collection_id`) REFERENCES `collections` (`id`);
ALTER TABLE `collection_posts` ADD FOREIGN KEY (`user_id`, `post_id`) REFERENCES `saves` (`user_id`, `post_id`) ON DELETE CASCADE;
ALTER TABLE `stories` ADD FOREIGN KEY (`author_id`) REFERENCES `users` (`id`);
ALTER TABLE `stories` ADD FOREIGN KEY (`removed_by_action_id`) REFERENCES `moderation_actions` (`id`);
ALTER TABLE `story_views` ADD FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`);
ALTER TABLE `story_views` ADD FOREIGN KEY (`viewer_id`) REFERENCES `users` (`id`);
ALTER TABLE `highlights` ADD FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`);
ALTER TABLE `highlight_stories` ADD FOREIGN KEY (`highlight_id`) REFERENCES `highlights` (`id`);
ALTER TABLE `highlight_stories` ADD FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`);
ALTER TABLE `notifications` ADD FOREIGN KEY (`recipient_id`) REFERENCES `users` (`id`);
ALTER TABLE `notifications` ADD FOREIGN KEY (`actor_id`) REFERENCES `users` (`id`);
ALTER TABLE `notifications` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `notifications` ADD FOREIGN KEY (`comment_id`) REFERENCES `comments` (`id`);
ALTER TABLE `notifications` ADD FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`);
ALTER TABLE `push_devices` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
ALTER TABLE `blocks` ADD FOREIGN KEY (`blocker_id`) REFERENCES `users` (`id`);
ALTER TABLE `blocks` ADD FOREIGN KEY (`blocked_id`) REFERENCES `users` (`id`);
ALTER TABLE `mutes` ADD FOREIGN KEY (`muter_id`) REFERENCES `users` (`id`);
ALTER TABLE `mutes` ADD FOREIGN KEY (`muted_id`) REFERENCES `users` (`id`);
ALTER TABLE `reports` ADD FOREIGN KEY (`reporter_id`) REFERENCES `users` (`id`);
ALTER TABLE `reports` ADD FOREIGN KEY (`reported_user_id`) REFERENCES `users` (`id`);
ALTER TABLE `reports` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `reports` ADD FOREIGN KEY (`comment_id`) REFERENCES `comments` (`id`);
ALTER TABLE `reports` ADD FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`);
ALTER TABLE `moderation_actions` ADD FOREIGN KEY (`report_id`) REFERENCES `reports` (`id`);
ALTER TABLE `moderation_actions` ADD FOREIGN KEY (`moderator_id`) REFERENCES `users` (`id`);
ALTER TABLE `moderation_actions` ADD FOREIGN KEY (`target_user_id`) REFERENCES `users` (`id`);
ALTER TABLE `moderation_actions` ADD FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`);
ALTER TABLE `moderation_actions` ADD FOREIGN KEY (`comment_id`) REFERENCES `comments` (`id`);
ALTER TABLE `moderation_actions` ADD FOREIGN KEY (`story_id`) REFERENCES `stories` (`id`);
ALTER TABLE `moderation_actions` ADD FOREIGN KEY (`reverses_action_id`) REFERENCES `moderation_actions` (`id`);SQL Server
CREATE TABLE [users] (
[id] uuid PRIMARY KEY,
[email] varchar(254) UNIQUE,
[phone] varchar(30) UNIQUE,
[password_hash] varchar(200),
[status] nvarchar(255) NOT NULL CHECK ([status] IN ('active', 'deactivated', 'suspended', 'banned', 'deleted')) DEFAULT 'active',
[is_staff] boolean NOT NULL DEFAULT (false),
[date_of_birth] date,
[created_at] timestamptz NOT NULL DEFAULT (now()),
[deactivated_at] timestamptz,
[deleted_at] timestamptz
)
GO
CREATE TABLE [profiles] (
[user_id] uuid PRIMARY KEY,
[username] varchar(30) UNIQUE NOT NULL,
[display_name] varchar(64),
[bio] varchar(150),
[website_url] varchar(300),
[avatar_storage_key] varchar(300),
[is_private] boolean NOT NULL DEFAULT (false),
[follower_count] integer NOT NULL DEFAULT (0),
[following_count] integer NOT NULL DEFAULT (0),
[post_count] integer NOT NULL DEFAULT (0),
[updated_at] timestamptz NOT NULL DEFAULT (now())
)
GO
CREATE TABLE [follows] (
[follower_id] uuid NOT NULL,
[followee_id] uuid NOT NULL,
[state] nvarchar(255) NOT NULL CHECK ([state] IN ('pending', 'accepted')),
[requested_at] timestamptz NOT NULL DEFAULT (now()),
[accepted_at] timestamptz,
PRIMARY KEY ([follower_id], [followee_id])
)
GO
CREATE TABLE [close_friends] (
[owner_id] uuid NOT NULL,
[friend_id] uuid NOT NULL,
[added_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([owner_id], [friend_id])
)
GO
CREATE TABLE [places] (
[id] uuid PRIMARY KEY,
[name] varchar(200) NOT NULL,
[latitude] numeric(9,6) NOT NULL,
[longitude] numeric(9,6) NOT NULL,
[city] varchar(120),
[country] char(2),
[provider_place_id] varchar(100) UNIQUE
)
GO
CREATE TABLE [posts] (
[id] uuid PRIMARY KEY,
[author_id] uuid NOT NULL,
[caption] text,
[place_id] uuid,
[status] nvarchar(255) NOT NULL CHECK ([status] IN ('processing', 'published', 'archived')) DEFAULT 'processing',
[comments_disabled] boolean NOT NULL DEFAULT (false),
[hide_like_count] boolean NOT NULL DEFAULT (false),
[like_count] integer NOT NULL DEFAULT (0),
[comment_count] integer NOT NULL DEFAULT (0),
[created_at] timestamptz NOT NULL DEFAULT (now()),
[edited_at] timestamptz,
[deleted_at] timestamptz,
[removed_by_action_id] uuid
)
GO
CREATE TABLE [post_media] (
[id] uuid PRIMARY KEY,
[post_id] uuid NOT NULL,
[position] smallint NOT NULL,
[media_type] nvarchar(255) NOT NULL CHECK ([media_type] IN ('photo', 'video')),
[original_storage_key] varchar(300) NOT NULL,
[width] integer NOT NULL,
[height] integer NOT NULL,
[duration_ms] integer,
[alt_text] varchar(500),
[blurhash] varchar(60),
[processing_status] nvarchar(255) NOT NULL CHECK ([processing_status] IN ('pending', 'processing', 'ready', 'failed')) DEFAULT 'pending'
)
GO
CREATE TABLE [media_variants] (
[id] uuid PRIMARY KEY,
[post_media_id] uuid NOT NULL,
[variant] nvarchar(255) NOT NULL CHECK ([variant] IN ('thumbnail_150', 'small_320', 'medium_640', 'large_1080', 'video_480p', 'video_720p', 'video_1080p')),
[content_type] varchar(50) NOT NULL,
[storage_key] varchar(300) UNIQUE NOT NULL,
[width] integer NOT NULL,
[height] integer NOT NULL,
[bytes] integer NOT NULL
)
GO
CREATE TABLE [photo_tags] (
[post_media_id] uuid NOT NULL,
[tagged_user_id] uuid NOT NULL,
[x] numeric(5,4) NOT NULL,
[y] numeric(5,4) NOT NULL,
[approved] boolean NOT NULL DEFAULT (true),
[created_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([post_media_id], [tagged_user_id])
)
GO
CREATE TABLE [hashtags] (
[id] uuid PRIMARY KEY,
[name] varchar(100) UNIQUE NOT NULL,
[post_count] integer NOT NULL DEFAULT (0),
[is_blocked] boolean NOT NULL DEFAULT (false),
[created_at] timestamptz NOT NULL DEFAULT (now())
)
GO
CREATE TABLE [post_hashtags] (
[post_id] uuid NOT NULL,
[hashtag_id] uuid NOT NULL,
[created_at] timestamptz NOT NULL,
PRIMARY KEY ([post_id], [hashtag_id])
)
GO
CREATE TABLE [mentions] (
[id] uuid PRIMARY KEY,
[mentioned_user_id] uuid NOT NULL,
[post_id] uuid,
[comment_id] uuid,
[created_at] timestamptz NOT NULL DEFAULT (now())
)
GO
CREATE TABLE [post_likes] (
[post_id] uuid NOT NULL,
[user_id] uuid NOT NULL,
[created_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([post_id], [user_id])
)
GO
CREATE TABLE [comments] (
[id] uuid PRIMARY KEY,
[post_id] uuid NOT NULL,
[author_id] uuid NOT NULL,
[parent_comment_id] uuid,
[body] varchar(2200) NOT NULL,
[like_count] integer NOT NULL DEFAULT (0),
[reply_count] integer NOT NULL DEFAULT (0),
[is_pinned] boolean NOT NULL DEFAULT (false),
[created_at] timestamptz NOT NULL DEFAULT (now()),
[edited_at] timestamptz,
[deleted_at] timestamptz,
[removed_by_action_id] uuid
)
GO
CREATE TABLE [comment_likes] (
[comment_id] uuid NOT NULL,
[user_id] uuid NOT NULL,
[created_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([comment_id], [user_id])
)
GO
CREATE TABLE [saves] (
[user_id] uuid NOT NULL,
[post_id] uuid NOT NULL,
[saved_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([user_id], [post_id])
)
GO
CREATE TABLE [collections] (
[id] uuid PRIMARY KEY,
[owner_id] uuid NOT NULL,
[name] varchar(100) NOT NULL,
[cover_post_id] uuid,
[created_at] timestamptz NOT NULL DEFAULT (now())
)
GO
CREATE TABLE [collection_posts] (
[collection_id] uuid NOT NULL,
[user_id] uuid NOT NULL,
[post_id] uuid NOT NULL,
[added_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([collection_id], [post_id])
)
GO
CREATE TABLE [stories] (
[id] uuid PRIMARY KEY,
[author_id] uuid NOT NULL,
[media_type] nvarchar(255) NOT NULL CHECK ([media_type] IN ('photo', 'video')),
[storage_key] varchar(300) NOT NULL,
[width] integer NOT NULL,
[height] integer NOT NULL,
[duration_ms] integer NOT NULL DEFAULT (5000),
[audience] nvarchar(255) NOT NULL CHECK ([audience] IN ('followers', 'close_friends')) DEFAULT 'followers',
[view_count] integer NOT NULL DEFAULT (0),
[created_at] timestamptz NOT NULL DEFAULT (now()),
[expires_at] timestamptz NOT NULL,
[deleted_at] timestamptz,
[removed_by_action_id] uuid
)
GO
CREATE TABLE [story_views] (
[story_id] uuid NOT NULL,
[viewer_id] uuid NOT NULL,
[viewed_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([story_id], [viewer_id])
)
GO
CREATE TABLE [highlights] (
[id] uuid PRIMARY KEY,
[owner_id] uuid NOT NULL,
[title] varchar(30) NOT NULL,
[cover_storage_key] varchar(300),
[position] smallint NOT NULL,
[created_at] timestamptz NOT NULL DEFAULT (now())
)
GO
CREATE TABLE [highlight_stories] (
[highlight_id] uuid NOT NULL,
[story_id] uuid NOT NULL,
[position] smallint NOT NULL,
PRIMARY KEY ([highlight_id], [story_id])
)
GO
CREATE TABLE [notifications] (
[id] uuid PRIMARY KEY,
[recipient_id] uuid NOT NULL,
[actor_id] uuid,
[kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('like', 'comment', 'reply', 'comment_like', 'mention', 'photo_tag', 'follow', 'follow_request', 'follow_accepted', 'moderation_notice')),
[post_id] uuid,
[comment_id] uuid,
[story_id] uuid,
[group_key] varchar(120),
[created_at] timestamptz NOT NULL DEFAULT (now()),
[read_at] timestamptz
)
GO
CREATE TABLE [push_devices] (
[id] uuid PRIMARY KEY,
[user_id] uuid NOT NULL,
[platform] nvarchar(255) NOT NULL CHECK ([platform] IN ('ios', 'android', 'web')),
[push_token] varchar(500) UNIQUE NOT NULL,
[app_version] varchar(20),
[last_seen_at] timestamptz NOT NULL,
[disabled_at] timestamptz
)
GO
CREATE TABLE [blocks] (
[blocker_id] uuid NOT NULL,
[blocked_id] uuid NOT NULL,
[created_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([blocker_id], [blocked_id])
)
GO
CREATE TABLE [mutes] (
[muter_id] uuid NOT NULL,
[muted_id] uuid NOT NULL,
[mute_posts] boolean NOT NULL DEFAULT (true),
[mute_stories] boolean NOT NULL DEFAULT (true),
[created_at] timestamptz NOT NULL DEFAULT (now()),
PRIMARY KEY ([muter_id], [muted_id])
)
GO
CREATE TABLE [reports] (
[id] uuid PRIMARY KEY,
[reporter_id] uuid NOT NULL,
[target_kind] nvarchar(255) NOT NULL CHECK ([target_kind] IN ('post', 'comment', 'story', 'account')),
[reported_user_id] uuid NOT NULL,
[post_id] uuid,
[comment_id] uuid,
[story_id] uuid,
[reason] nvarchar(255) NOT NULL CHECK ([reason] IN ('spam', 'harassment', 'hate_speech', 'nudity', 'violence', 'self_harm', 'impersonation', 'scam', 'intellectual_property', 'other')),
[details] text,
[status] nvarchar(255) NOT NULL CHECK ([status] IN ('open', 'in_review', 'actioned', 'dismissed')) DEFAULT 'open',
[created_at] timestamptz NOT NULL DEFAULT (now()),
[resolved_at] timestamptz
)
GO
CREATE TABLE [moderation_actions] (
[id] uuid PRIMARY KEY,
[report_id] uuid,
[moderator_id] uuid,
[action] nvarchar(255) NOT NULL CHECK ([action] IN ('remove_content', 'restore_content', 'warn_user', 'suspend_user', 'ban_user', 'reinstate_user')),
[target_user_id] uuid NOT NULL,
[post_id] uuid,
[comment_id] uuid,
[story_id] uuid,
[policy_code] varchar(50) NOT NULL,
[reason] text,
[expires_at] timestamptz,
[reverses_action_id] uuid,
[created_at] timestamptz NOT NULL DEFAULT (now())
)
GO
CREATE INDEX [follows_index_0] ON [follows] ("followee_id", "state")
GO
CREATE INDEX [posts_index_1] ON [posts] ("author_id", "created_at")
GO
CREATE UNIQUE INDEX [post_media_index_2] ON [post_media] ("post_id", "position")
GO
CREATE UNIQUE INDEX [media_variants_index_3] ON [media_variants] ("post_media_id", "variant", "content_type")
GO
CREATE INDEX [photo_tags_index_4] ON [photo_tags] ("tagged_user_id")
GO
CREATE INDEX [post_hashtags_index_5] ON [post_hashtags] ("hashtag_id", "created_at")
GO
CREATE INDEX [mentions_index_6] ON [mentions] ("mentioned_user_id")
GO
CREATE INDEX [post_likes_index_7] ON [post_likes] ("user_id", "created_at")
GO
CREATE INDEX [comments_index_8] ON [comments] ("post_id", "parent_comment_id", "created_at")
GO
CREATE INDEX [saves_index_9] ON [saves] ("user_id", "saved_at")
GO
CREATE UNIQUE INDEX [collections_index_10] ON [collections] ("owner_id", "name")
GO
CREATE INDEX [stories_index_11] ON [stories] ("author_id", "expires_at")
GO
CREATE INDEX [notifications_index_12] ON [notifications] ("recipient_id", "created_at")
GO
CREATE INDEX [notifications_index_13] ON [notifications] ("recipient_id", "group_key")
GO
CREATE INDEX [blocks_index_14] ON [blocks] ("blocked_id")
GO
CREATE INDEX [reports_index_15] ON [reports] ("status", "created_at")
GO
CREATE INDEX [reports_index_16] ON [reports] ("reported_user_id")
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Login identity and account state. Everything public-facing lives on profiles, so credentials are never part of a query that renders a page.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'users';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'E.164 format. An account needs an email or a phone (a CHECK constraint), not both',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'users',
@level2type = N'Column', @level2name = 'phone';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for accounts that sign in only with a social login or a one-time code',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'users',
@level2type = N'Column', @level2name = 'password_hash';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Can work the moderation queue. A role on the same account, never a separate login',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'users',
@level2type = N'Column', @level2name = 'is_staff';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Minimum age checks and teen account defaults',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'users',
@level2type = N'Column', @level2name = 'date_of_birth';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'User-initiated pause: content hidden, fully restorable',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'users',
@level2type = N'Column', @level2name = 'deactivated_at';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Soft delete. A purge job erases personal data after the grace period; the row then keeps only its id',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'users',
@level2type = N'Column', @level2name = 'deleted_at';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The public face of an account, one to one with users. The counters are denormalized for profile headers and reconciled from their source rows by a background job.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'profiles';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Stored lowercase; uniqueness is case-insensitive (citext, or a unique index on lower(username))',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'profiles',
@level2type = N'Column', @level2name = 'username';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Object storage key. Avatar sizes are derived by naming convention',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'profiles',
@level2type = N'Column', @level2name = 'avatar_storage_key';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Private accounts turn every new follow into a request',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'profiles',
@level2type = N'Column', @level2name = 'is_private';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE. Authoritative: accepted rows in follows',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'profiles',
@level2type = N'Column', @level2name = 'follower_count';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE. Authoritative: accepted rows in follows',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'profiles',
@level2type = N'Column', @level2name = 'following_count';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE. Authoritative: live rows in posts',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'profiles',
@level2type = N'Column', @level2name = 'post_count';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One row per directed follow. A request to a private account is the same row in state pending, so approving is an UPDATE and every visibility check reads only state = accepted. Declining deletes the row.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'follows';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'pending until a private account approves; accepted straight away otherwise',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'follows',
@level2type = N'Column', @level2name = 'state';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A private audience list for stories. The friend is never told they were added or removed.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'close_friends';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Locations a post can be tagged at. A place page lists posts by place_id, so coordinates are stored once.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'places';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Id from the maps provider the location was picked from, so re-picking it finds this row',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'places',
@level2type = N'Column', @level2name = 'provider_place_id';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A post is a container: caption, place and settings. The photos and videos are post_media rows, which is what makes a carousel simply a post with more than one media row.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'posts';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Raw text as typed. Hashtags and mentions are parsed out of it on every save',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'posts',
@level2type = N'Column', @level2name = 'caption';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE of count(post_likes)',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'posts',
@level2type = N'Column', @level2name = 'like_count';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE of live comments',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'posts',
@level2type = N'Column', @level2name = 'comment_count';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Soft delete by the author',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'posts',
@level2type = N'Column', @level2name = 'deleted_at';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set when moderation removes the post. Clearing it restores the post exactly as it was',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'posts',
@level2type = N'Column', @level2name = 'removed_by_action_id';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One photo or video in a post, in carousel order. Bytes live in object storage; the database holds keys and metadata only.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_media';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '0-based order in the carousel',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_media',
@level2type = N'Column', @level2name = 'position';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The upload as received. Kept private; nothing serves it directly',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_media',
@level2type = N'Column', @level2name = 'original_storage_key';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Videos only',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_media',
@level2type = N'Column', @level2name = 'duration_ms';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Written by the author or generated; read aloud by screen readers',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_media',
@level2type = N'Column', @level2name = 'alt_text';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Tiny placeholder string the app paints while the real image loads',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_media',
@level2type = N'Column', @level2name = 'blurhash';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Resized and transcoded renditions written by the media pipeline. A feed asks for the variant that fits the screen, so a phone on a slow network never downloads the original.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'media_variants';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. image/webp, image/jpeg, video/mp4',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'media_variants',
@level2type = N'Column', @level2name = 'content_type';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'People tagged ON a photo, at a point. Different from a mention, which lives in text.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'photo_tags';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '0 to 1, a fraction of the width, so the tag lands on the same spot in every variant',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'photo_tags',
@level2type = N'Column', @level2name = 'x';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = '0 to 1, a fraction of the height',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'photo_tags',
@level2type = N'Column', @level2name = 'y';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'False while waiting, for users who review tags manually',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'photo_tags',
@level2type = N'Column', @level2name = 'approved';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One row per distinct tag ever used.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'hashtags';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Lowercased, without the #. Display casing comes from the caption itself',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'hashtags',
@level2type = N'Column', @level2name = 'name';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE of post_hashtags rows',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'hashtags',
@level2type = N'Column', @level2name = 'post_count';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Blocked tags are not searchable and their page shows nothing',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'hashtags',
@level2type = N'Column', @level2name = 'is_blocked';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Derived from the caption on every save: the caption is the source, these rows are its index. Editing a caption rewrites the set.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_hashtags';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Copied from the post, so a hashtag page sorts by time without joining posts',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_hashtags',
@level2type = N'Column', @level2name = 'created_at';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = '@username references parsed from a caption or a comment. Exactly one of post_id and comment_id is set (a CHECK constraint). Stored by user id, so a username change never breaks the link.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'mentions';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The authoritative likes. posts.like_count is a cache incremented in the same transaction and reconciled from these rows; when the two disagree, these rows win.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'post_likes';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Threaded comments. A soft-deleted parent keeps its replies attached instead of orphaning them.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'comments';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for a top-level comment. Replies are one level deep: replying to a reply points at the same top-level comment and mentions the person',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'comments',
@level2type = N'Column', @level2name = 'parent_comment_id';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE of comment_likes',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'comments',
@level2type = N'Column', @level2name = 'like_count';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE of live replies',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'comments',
@level2type = N'Column', @level2name = 'reply_count';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The post author can pin a few comments to the top',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'comments',
@level2type = N'Column', @level2name = 'is_pinned';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Soft delete. Replies stay attached and the parent renders as deleted',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'comments',
@level2type = N'Column', @level2name = 'deleted_at';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Likes on comments. comments.like_count is their cache.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'comment_likes';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Bookmarks. Private to the saver; the author only ever sees an aggregate count.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'saves';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Optional named folders over saved posts.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'collections';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null means use the most recently added post',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'collections',
@level2type = N'Column', @level2name = 'cover_post_id';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Which saved posts are filed in which collection. The composite foreign key to saves cascades, so unsaving a post removes it from every collection at once.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'collection_posts';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The collection owner, repeated so the row can reference the save it files',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'collection_posts',
@level2type = N'Column', @level2name = 'user_id';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Ephemeral posts. An expired story leaves the tray but stays in the author archive, which is how it can still be added to a highlight.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'stories';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'How long it shows. Videos use their own length, up to a cap',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'stories',
@level2type = N'Column', @level2name = 'duration_ms';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'CACHE of story_views',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'stories',
@level2type = N'Column', @level2name = 'view_count';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'created_at + 24 hours. Feeds filter on it; no job deletes anything at expiry',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'stories',
@level2type = N'Column', @level2name = 'expires_at';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Who saw a story, visible only to its author. Purged a couple of days after expiry; the archive keeps stories.view_count.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'story_views';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Named sets of past stories pinned to a profile. They outlive expiry because they point at story rows that were never deleted.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'highlights';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Order on the profile',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'highlights',
@level2type = N'Column', @level2name = 'position';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Which stories are in a highlight, in order.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'highlight_stories';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The in-app activity feed. One row per event, grouped at read time, so an undone like deletes exactly its own row.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'notifications';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Who did it; null for system notices',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'notifications',
@level2type = N'Column', @level2name = 'actor_id';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. like:post:<id>. The inbox collapses rows sharing a key into "sam and 41 others liked your post"',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'notifications',
@level2type = N'Column', @level2name = 'group_key';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Where to deliver push notifications. One user, many phones and tablets.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'push_devices';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Issued by the OS push service. It rotates, so the app upserts it on every launch',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'push_devices',
@level2type = N'Column', @level2name = 'push_token';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set when the push service reports the token invalid',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'push_devices',
@level2type = N'Column', @level2name = 'disabled_at';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A block removes any follow in both directions and hides each person from the other everywhere: feed, search, comments, stories. Every read path filters on it.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'blocks';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A quiet unfollow: the follow stays, the content stops appearing, and the muted person is never told.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'mutes';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'User reports. Many reports about one post are many rows; the queue groups them by target so a moderator reviews it once.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'reports';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Always set: the author of the reported content, or the account itself',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'reports',
@level2type = N'Column', @level2name = 'reported_user_id';
GO
EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Append-only log of everything moderation did. A moderator never hard-deletes content: posts, comments and stories point back here through removed_by_action_id.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'moderation_actions';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for proactive actions, e.g. from an automated classifier',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'moderation_actions',
@level2type = N'Column', @level2name = 'report_id';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'A staff user; null when an automated system acted',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'moderation_actions',
@level2type = N'Column', @level2name = 'moderator_id';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Which rule was broken, e.g. HARASSMENT_2. Quoted in the notice the user receives',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'moderation_actions',
@level2type = N'Column', @level2name = 'policy_code';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'For time-limited suspensions',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'moderation_actions',
@level2type = N'Column', @level2name = 'expires_at';
GO
EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'A successful appeal is a new restore action pointing at the one it undoes',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'moderation_actions',
@level2type = N'Column', @level2name = 'reverses_action_id';
GO
ALTER TABLE [profiles] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [follows] ADD FOREIGN KEY ([follower_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [follows] ADD FOREIGN KEY ([followee_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [close_friends] ADD FOREIGN KEY ([owner_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [close_friends] ADD FOREIGN KEY ([friend_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [posts] ADD FOREIGN KEY ([author_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [posts] ADD FOREIGN KEY ([place_id]) REFERENCES [places] ([id])
GO
ALTER TABLE [posts] ADD FOREIGN KEY ([removed_by_action_id]) REFERENCES [moderation_actions] ([id])
GO
ALTER TABLE [post_media] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [media_variants] ADD FOREIGN KEY ([post_media_id]) REFERENCES [post_media] ([id])
GO
ALTER TABLE [photo_tags] ADD FOREIGN KEY ([post_media_id]) REFERENCES [post_media] ([id])
GO
ALTER TABLE [photo_tags] ADD FOREIGN KEY ([tagged_user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [post_hashtags] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [post_hashtags] ADD FOREIGN KEY ([hashtag_id]) REFERENCES [hashtags] ([id])
GO
ALTER TABLE [mentions] ADD FOREIGN KEY ([mentioned_user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [mentions] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [mentions] ADD FOREIGN KEY ([comment_id]) REFERENCES [comments] ([id])
GO
ALTER TABLE [post_likes] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [post_likes] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [comments] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [comments] ADD FOREIGN KEY ([author_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [comments] ADD FOREIGN KEY ([parent_comment_id]) REFERENCES [comments] ([id])
GO
ALTER TABLE [comments] ADD FOREIGN KEY ([removed_by_action_id]) REFERENCES [moderation_actions] ([id])
GO
ALTER TABLE [comment_likes] ADD FOREIGN KEY ([comment_id]) REFERENCES [comments] ([id])
GO
ALTER TABLE [comment_likes] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [saves] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [saves] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [collections] ADD FOREIGN KEY ([owner_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [collections] ADD FOREIGN KEY ([cover_post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [collection_posts] ADD FOREIGN KEY ([collection_id]) REFERENCES [collections] ([id])
GO
ALTER TABLE [collection_posts] ADD FOREIGN KEY ([user_id], [post_id]) REFERENCES [saves] ([user_id], [post_id]) ON DELETE CASCADE
GO
ALTER TABLE [stories] ADD FOREIGN KEY ([author_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [stories] ADD FOREIGN KEY ([removed_by_action_id]) REFERENCES [moderation_actions] ([id])
GO
ALTER TABLE [story_views] ADD FOREIGN KEY ([story_id]) REFERENCES [stories] ([id])
GO
ALTER TABLE [story_views] ADD FOREIGN KEY ([viewer_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [highlights] ADD FOREIGN KEY ([owner_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [highlight_stories] ADD FOREIGN KEY ([highlight_id]) REFERENCES [highlights] ([id])
GO
ALTER TABLE [highlight_stories] ADD FOREIGN KEY ([story_id]) REFERENCES [stories] ([id])
GO
ALTER TABLE [notifications] ADD FOREIGN KEY ([recipient_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [notifications] ADD FOREIGN KEY ([actor_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [notifications] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [notifications] ADD FOREIGN KEY ([comment_id]) REFERENCES [comments] ([id])
GO
ALTER TABLE [notifications] ADD FOREIGN KEY ([story_id]) REFERENCES [stories] ([id])
GO
ALTER TABLE [push_devices] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [blocks] ADD FOREIGN KEY ([blocker_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [blocks] ADD FOREIGN KEY ([blocked_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [mutes] ADD FOREIGN KEY ([muter_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [mutes] ADD FOREIGN KEY ([muted_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [reports] ADD FOREIGN KEY ([reporter_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [reports] ADD FOREIGN KEY ([reported_user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [reports] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [reports] ADD FOREIGN KEY ([comment_id]) REFERENCES [comments] ([id])
GO
ALTER TABLE [reports] ADD FOREIGN KEY ([story_id]) REFERENCES [stories] ([id])
GO
ALTER TABLE [moderation_actions] ADD FOREIGN KEY ([report_id]) REFERENCES [reports] ([id])
GO
ALTER TABLE [moderation_actions] ADD FOREIGN KEY ([moderator_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [moderation_actions] ADD FOREIGN KEY ([target_user_id]) REFERENCES [users] ([id])
GO
ALTER TABLE [moderation_actions] ADD FOREIGN KEY ([post_id]) REFERENCES [posts] ([id])
GO
ALTER TABLE [moderation_actions] ADD FOREIGN KEY ([comment_id]) REFERENCES [comments] ([id])
GO
ALTER TABLE [moderation_actions] ADD FOREIGN KEY ([story_id]) REFERENCES [stories] ([id])
GO
ALTER TABLE [moderation_actions] ADD FOREIGN KEY ([reverses_action_id]) REFERENCES [moderation_actions] ([id])
GOHow to use this template
- Open the ER diagram to see the tables and how they relate.
- Copy it into your workspace. You get your own editable copy, in DBML.
- Export it as PostgreSQL, MySQL, or SQL Server DDL, or as Prisma, Drizzle, SQLAlchemy, TypeScript, Zod or C# models.
The copy opens in DB Planner’s ER diagram tool, with the schema as text in the DBML editor beside it. Have a database already? Turn its SQL into an ER diagram instead.
Frequently asked questions
- Which table is at the centre of the Photo Sharing database schema?
- users. It is referenced by 19 other tables: blocks, close_friends, collections, comment_likes, comments and 14 more.
- How is blocks related to users?
- blocks.blocked_id references users.id, a many to one relationship. All 60 relationships in the schema are listed on this page.
- Which table in the Photo Sharing database schema has the most columns?
- moderation_actions, with 13 columns, followed by posts (13) and stories (13). The schema has 192 columns across 28 tables.
- Can I export it to PostgreSQL or MySQL?
- Yes. Every DB Planner template exports as DBML, as PostgreSQL, MySQL, or SQL Server DDL, and as C# models.
- Is this template free?
- Yes. You can open it in the browser without an account, and copy it into your own workspace with a free DB Planner account.
- Can I change it after copying it?
- Yes. A copy is yours: you can rename tables, add columns, and export it. Changes the author makes later do not affect your copy.