Schema, Joins & Associations - darrenyong/aprtr GitHub Wiki
Table of Contents
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 referenceUsers
: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 referenceUsers
: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 referenceUsers
:id
photo_id
will referencePhotos
: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 manyphotos
photos
can belong to manyalbums
album_id
will referenceAlbums
:id
photo_id
will referencePhotos
: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 manytags
tags
belong to manyphotos
photo_id
will referencePhotos
:id
tag_id
will referenceTags
: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 |