Schema - harrisonlhl123/facebook-clone GitHub Wiki

Postgres Database Schema

Users:

name type validations
id bigInt not null, primary key
email string not null, indexed, unique
first_name string not null
last_name string not null
birthday date not null
gender string not null
password_digest string not null
session_token string not null, indexed, unique
created_at datetime not null
updated_at datetime not null
  • email indexed and unique
  • session_token indexed and unique
  • has_many :posts
  • has_many :comments
  • has_many :friends

Posts:

name type validations
id bigInt not null, primary key
body text not null
author_id bigInt not null, indexed, foreign key
created_at datetime not null
updated_at datetime not null
  • author_id is used to reference a user
  • belongs_to user
  • has_many comments

Comments:

name type validations
id integer not null, primary key
user_id integer not null, indexed, foreign key
post_id integer not null, indexed, foreign key
body string not null
created_at datetime not null
updated_at datetime not null
  • user_id is used to reference a user
  • post_id is used to reference a post
  • belongs_to user
  • belongs_to post

Friending:

name type validations
id bigInt not null, primary key
user_id bigInt not null, indexed, foreign key
friend_id bigInt not null, indexed, foreign key
created_at datetime not null
updated_at datetime not null
  • user_id is used to reference a user
  • friend_id is used to reference another user
  • belongs_to user
  • belongs_to friend
  • [user_id, friend_id] are indexed and unique