Skip to content
Discussion options

You must be logged in to vote

Single self-referencing table all the way; Don't overthink this, it's a solved pattern.

CREATE TABLE comments (
  id BIGINT PRIMARY KEY,
  post_id BIGINT NOT NULL,
  parent_id BIGINT NULL, -- references comments.id
  user_id BIGINT NOT NULL,
  content TEXT NOT NULL,
  created_at TIMESTAMP,
  deleted_at TIMESTAMP NULL, -- soft delete
  path VARCHAR(255) -- materialized path like "1.5.12"
);

-- Essential indexes
CREATE INDEX idx_comments_post_parent ON comments(post_id, parent_id);
CREATE INDEX idx_comments_path ON comments(post_id, path);

Key decisions:

  1. Threading: Cap at 3-4 levels max. Beyond that, UX becomes terrible anyway. Either flatten or show "continue thread" links.
  2. Querying: Ma…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@Moyosof
Comment options

@CollatzConjecture
Comment options

Answer selected by Moyosof
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Ask and answer questions about GitHub features and usage
3 participants