comments table
The comments table in the Reddit Clone Database Schema schema has 24 columns, keyed by id, with 5 foreign keys to posts, users, comments and removal_reasons. 1 column in comment_votes references it.
tree_path is the materialised ancestry, so an entire thread is ONE prefix range query on (post_id, tree_path) rather than a recursive CTE per expand. parent_id is kept alongside it because the path alone cannot be enforced by a foreign key. best_score is the Wilson lower bound, which is why a comment with 9 of 10 upvotes outranks one with 1 of 1. is_collapsed is written by crowd control rather than recomputed per viewer.
Columns (24)
| Column | Type | Constraints | Description |
|---|---|---|---|
| id | uuid | primary key | |
| post_id | uuid | not null, references posts.id | |
| author_id | uuid | references users.id | |
| parent_id | uuid | references comments.id | |
| tree_path | varchar(1000) | not null | |
| depth | smallint | not null | |
| body_md | text | not null | |
| upvotes | integer | not null | |
| downvotes | integer | not null | |
| score | integer | not null | |
| best_score | double | not null | |
| controversial_score | double | not null | |
| reply_count | integer | not null | |
| award_count | integer | not null | |
| is_submitter | boolean | not null | |
| is_stickied | boolean | not null | |
| is_collapsed | boolean | not null | |
| distinguished_as | distinguish_kind | ||
| 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 (4)
(post_id, tree_path)(post_id, best_score)(parent_id, best_score)(author_id, created_at)
Relationships
comments.post_idreferencesposts.idcomments.author_idreferencesusers.idcomments.parent_idreferencescomments.idcomments.removed_byreferencesusers.idcomments.removal_reason_idreferencesremoval_reasons.idcomment_votes.comment_idreferencescomments