Skip to content

Database Schema

Wei-Yang Lin edited this page Sep 20, 2021 · 10 revisions

database configuration[place holder]

* "database": "mora_development"
* "username": "mora_development_app",
* "password": "password",

migration/seeding sequence[place holder]

* categories
* users
* questions
* questions_categories
* answers
* comments

users

column name data type details
id int pk, not null, unique
user_name varchar(50) not null, unique
email varchar(255) not null, unique
occupation varchar(100)
hashed_password varchar(255) not null
created_at timestamp not null
updated_at timestamp not null

questions

column name data type details
id int pk, not null, unique
user_id int fk, references users table(id), not null
title text not null, max length 1000
created_at timestamp not null
updated_at timestamp not null
  • user_id is foreign key, references user table(id), relation is one to many
    • One question belongs to one user
    • One user has many questions

answers

column name data type details
id int pk, not null, unique
user_id int fk, references users table(id), not null
question_id int fk, references questions table(id), not null
content text not null
created_at timestamp not null
updated_at timestamp not null
  • user_id is foreign key, references user table(id), relation is one to many
    • One answer belongs to one user
    • One user has many answers
  • question_id is foreign key, references question table(id), relation is one to many
    • One answer belongs to one question
    • One question has many answers

questions_votes

column name data type details
id int pk, not null, unique
user_id int fk, references users table(id), not null
question_id int fk, references questions table(id), not null
created_at timestamp not null
updated_at timestamp not null
  • user_id is foreign key, references user table(id), relation is one to many
    • One vote belongs to one user
    • One user has many votes
  • question_id is foreign key, references question table(id), relation is one to many
    • One vote belongs to one question
    • One question has many votes

answers_votes

column name data type details
id int pk, not null, unique
user_id int fk, references users table(id), not null
answer_id int fk, references answers table(id), not null
created_at timestamp not null
updated_at timestamp not null
  • user_id is foreign key, references user table(id), relation is one to many
    • One vote belongs to one user
    • One user has many votes
  • answer_id is foreign key, references answer table(id), relation is one to many
    • One vote belongs to one answer
    • One answer has many votes

BONUS BELOW

comments

column name data type details
id int pk, not null, unique
answer_id int fk, references answers table(id), not null
user_id int fk, references users table(id), not null
content text not null
created_at timestamp not null
updated_at timestamp not null
  • user_id is foreign key, references user table(id), relation is one to many
    • One comments belongs to one user
    • One user has many comments
  • answer_id is foreign key, references answers table(id), relation is one to many
    • One comment belongs to one answer
    • One answer has many comments

categories

column name data type details
id int pk, not null, unique
name enum not null
created_at timestamp not null
updated_at timestamp not null

question_categories

column name data type details
id int pk, not null, unique
question_id int fk, references questions table(id), not null
category_id int fk, references categories table(id), not null
created_at timestamp not null
updated_at timestamp not null
  • This is a join table, define the relationship between questions and categories is many to many
    • Question.belongsToMany(Category, { through: "questions_categories foreignKey: "question_id", otherKey: "category_id" });
    • Category.belongsToMany(Question, { through: "questions_categories", foreignKey: "category_id", otherKey: "question_id" });

Clone this wiki locally