Schema, Joins & Associations - darrenyong/aprtr GitHub Wiki

Table of Contents

  1. Database Schema
  2. Joins Tables
  3. Associations

Database Schema

Users

Column Data Type Details
id integer not null, primary key
username string not null, unique, indexed
email string not null, unique, indexed
password_digest string not null
session_token string not null, unique, indexed
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

Photos

Column Data Type Details
id integer not null, primary key
title string
description text
uploader_id integer not null, foreign key, indexed
created_at datetime not null
updated_at datetime not null
  • index on user_id
  • uploader will reference Users:id

Albums

Column Data Type Details
id integer not null, primary key
title string not null
description text
user_id integer not null, foreign key, indexed
created_at datetime not null
updated_at datetime not null
  • index on user_id
  • user_id will reference Users:id

Comments

Column Data Type Details
id integer not null, primary key
body text not null
author_id integer not null, foreign key, indexed
photo_id integer not null, foreign key, indexed
created_at datetime not null
updated_at datetime not null
  • index on user_id
  • index on photo_id
  • author_id will reference Users:id
  • photo_id will reference Photos:id

Tags

Column Data Type Details
id integer not null, primary key
name string not null, unique
created_at datetime not null
updated_at datetime not null
  • index on name

Joins Tables

Album Photos

Column Data Type Details
id integer not null, primary key
album_id integer not null, foreign key
photo_id integer not null, foreign key
  • albums can have many photos
  • photos can belong to many albums
  • album_id will reference Albums:id
  • photo_id will reference Photos:id

Photo Tags

Column Data Type Details
id integer not null, primary key
photo_id integer not null, foreign key
tag_id integer not null, foreign key
  • photos can have many tags
  • tags belong to many photos
  • photo_id will reference Photos:id
  • tag_id will reference Tags:id

Associations

User

Association Primary Key Foreign Key Class Name
has_many: :photos :id :user_id Photo
has_many: :comments :id :user_id Comment
has_many: :albums :id :user_id Album

Photo

Association Primary Key Foreign Key Class Name
belongs_to_many: :albums :id :album_id Album
belongs_to: :user :id :user_id User
has_many: :comments :id :photo_id Comment
has_many: :tags :id :photo_id Tag

Album

Association Primary Key Foreign Key Class Name
belongs_to: :user :id :user_id User
has_many: :photos :id :album_id Photo

Comment

Association Primary Key Foreign Key Class Name
belongs_to: :user :id :user_id User
belongs_to: :photos :id :photo_id Photo

Tag

Association Primary Key Foreign Key Class Name
belongs_to_many: :photos :id :photo_id Photo