posts table
The posts table in the Reddit Clone Database Schema schema has 41 columns, keyed by id, with 8 foreign keys to polls, subreddits, users, flairs and 3 more. 10 columns in post_media, comments, scheduled_posts, collection_posts and 6 more reference it.
The five score columns are the whole ranking design: each Reddit sort is a DIFFERENT function of the same votes, so each gets its own indexed column written when a vote lands. (subreddit_id, is_stickied, hot_score) IS the front page query. removed_at is a moderator and deleted_at is the author, which is exactly the difference between [removed] and [deleted] - collapsing them is the most common mistake in a Reddit clone. is_contest_mode randomises comment order and hides scores, which is why it belongs on the post rather than in a setting. author_id is nullable so a deleted account leaves the post standing.
Columns (41)
| Column | Type | Constraints | Description |
|---|---|---|---|
| id | uuid | primary key, references polls.post_id | |
| subreddit_id | uuid | not null, references subreddits.id | |
| author_id | uuid | references users.id | |
| kind | post_kind | not null | |
| title | varchar(300) | not null | |
| slug | varchar(120) | not null | |
| url | varchar(2000) | ||
| domain | varchar(255) | ||
| body_md | text | ||
| thumbnail_url | varchar(500) | ||
| flair_id | uuid | references flairs.id | |
| flair_text_override | varchar(64) | ||
| crosspost_parent_id | uuid | references posts.id | |
| collection_id | uuid | references collections.id | |
| is_nsfw | boolean | not null | |
| is_spoiler | boolean | not null | |
| is_original_content | boolean | not null | |
| is_locked | boolean | not null | |
| is_stickied | boolean | not null | |
| is_archived | boolean | not null | |
| is_contest_mode | boolean | not null | |
| distinguished_as | distinguish_kind | ||
| is_promoted | boolean | not null | |
| upvotes | integer | not null | |
| downvotes | integer | not null | |
| score | integer | not null | |
| display_seed | integer | not null | |
| upvote_ratio | double | not null | |
| comment_count | integer | not null | |
| award_count | integer | not null | |
| hot_score | double | not null | |
| best_score | double | not null | |
| top_score | integer | not null | |
| controversial_score | double | not null | |
| rising_score | double | not null | |
| created_at | timestamptz | not null | |
| edited_at | timestamptz | ||
| removed_at | timestamptz | ||
| removed_by | uuid | references users.id | |
| removal_reason_id | uuid | references removal_reasons.id | |
| deleted_at | timestamptz |
Indexes (10)
(subreddit_id, is_stickied, hot_score)(subreddit_id, top_score)(subreddit_id, created_at)(subreddit_id, rising_score)(subreddit_id, slug)unique(author_id, created_at)(crosspost_parent_id)(collection_id)(domain)(is_promoted, created_at)
Relationships
posts.idreferencespolls.post_idposts.subreddit_idreferencessubreddits.idposts.author_idreferencesusers.idposts.flair_idreferencesflairs.idposts.crosspost_parent_idreferencesposts.idposts.collection_idreferencescollections.idposts.removed_byreferencesusers.idposts.removal_reason_idreferencesremoval_reasons.idpost_media.post_idreferencespostscomments.post_idreferencespostsscheduled_posts.published_post_idreferencespostscollection_posts.post_idreferencespostshidden_posts.post_idreferencespostspost_votes.post_idreferencespostspost_rankings.post_idreferencespostspost_views.post_idreferencespostspost_insights_daily.post_idreferencespostsads.post_idreferencesposts