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)

Columns of the comments table
ColumnTypeConstraintsDescription
iduuidprimary key
post_iduuidnot null, references posts.id
author_iduuidreferences users.id
parent_iduuidreferences comments.id
tree_pathvarchar(1000)not null
depthsmallintnot null
body_mdtextnot null
upvotesintegernot null
downvotesintegernot null
scoreintegernot null
best_scoredoublenot null
controversial_scoredoublenot null
reply_countintegernot null
award_countintegernot null
is_submitterbooleannot null
is_stickiedbooleannot null
is_collapsedbooleannot null
distinguished_asdistinguish_kind
created_attimestamptznot null
edited_attimestamptz
removed_attimestamptz
removed_byuuidreferences users.id
removal_reason_iduuidreferences removal_reasons.id
deleted_attimestamptz

Indexes (4)

  • (post_id, tree_path)
  • (post_id, best_score)
  • (parent_id, best_score)
  • (author_id, created_at)

Relationships