Skip to content
Kevin Chen edited this page Mar 21, 2022 · 9 revisions

Postgres Database 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 posts
  • has_many follows

posts

column name data type details
id integer not null, primary key
pet_name string not null
pet_gender string not null
pet_breed string not null
pet_age integer not null
author_id integer not null, indexed, foreign key
created_at datetime not null
updated_at datetime not null
  • author_id references users
  • index on author_id
  • belongs_to author

follows

column name data type details
id integer not null, primary key
user_id integer not null, indexed, foreign key
post_id integer not null, indexed, foreign key
created_at datetime not null
updated_at datetime not null
  • user_id references users
  • post_id references posts
  • index on [:post_id, :user_id], unique: true
  • belongs_to user
  • belongs_to post

messages

column name data type details
id integer not null, primary key
body string not null
user_id integer not null, indexed, foreign key
receiver_id integer not null, indexed, foreign key
created_at datetime not null
updated_at datetime not null
  • user_id references user
  • receiver_id references user
  • index on [:post_id, :user_id], unique: true
  • belongs_to user
  • belongs_to user

// We don't need a separate index for post_id or user_id because the first index adds it for us.
Clone this wiki locally