Database Schema - dmurchison/stuck_overclone_app GitHub Wiki

PostgreSQL Database Tables Schema

users

column name data type details
id integer not null, primary key
username string not null, indexed, unique
email string not null, indexed, unique
password_digest string not null
session_token string not null, indexed, unique
created_at datetime not null
updated_at datetime not null
  • index on username, unique: true
  • index on email, unique: true
  • index on session_token, unique: true
  • has_many questions
  • has_many answers
  • has_many comments
  • has_many votes

questions

column name data type details
id integer not null, primary key
title string not null
body text not null
author_id integer not null, indexed, foreign key
created_at datetime not null
updated_at datetime not null
  • author_id references users
  • parent_question_id references questions
  • index on author_id
  • index on parent_question_id
  • belongs_to author
  • has_many answers
  • has_many comments
  • has_many votes

answers

column name data type details
id integer not null, primary key
body text not null
author_id integer not null, indexed, foreign key
question_id integer not null, indexed, foreign key
created_at datetime not null
updated_at datetime not null
  • author_id references users
  • question_id references questions
  • index on [:question_id, :author_id], unique: true
  • belongs_to author
  • belongs_to question
  • has_many comments
  • has_many votes

comments

column name data type details
id integer not null, primary key
body text not null
commenter_id integer not null, indexed, foreign key
question_id integer not null, indexed, foreign key
answer_id integer not null, indexed, foreign key
parent_comment_id integer indexed, foreign key
created_at datetime not null
updated_at datetime not null
  • commenter_id references users
  • question_id references questions
  • answer_id references answers
  • parent_comment_id references comments
  • index on commenter_id
  • belongs_to question
  • belongs_to answer
  • belongs_to comment
  • has_many comments

votes

column name data type details
id integer not null, primary key
voter_id integer not null, indexed, foreign key
question_id integer not null, indexed, foreign key
answer_id integer not null, indexed, foreign key
vote boolean not null
created_at datetime not null
updated_at datetime not null
  • voter_id references users
  • question_id references questions
  • answer_id references answers
  • vote is true for an upvote and false for a downvote
  • index on [:voter_id, :question_id, :answer_id], unique: true
  • belongs_to user
  • belongs_to question
  • belongs_to answer