フリマアプリのDB設計
プログラミングスクールの最終課題でフリマアプリを制作したので、そのDBを公開します。
全部で7テーブルです。(かなり少なくまとめました、追加実装のコメントテーブル等は無し)
途中でDB設計を何度も変更して手間がかかってしまったので、最初にしっかり考えてから制作に取り掛かることをお勧めします!
usersテーブル
| Column | Type | Options |
|---|---|---|
| nickname | string | null: false |
| string | null: false | |
| encrypted_password | string | null: false |
| user_image | string | |
| introduction | text | |
| family_name | string | null: false |
| first_name | string | null: false |
| family_name_kana | string | null: false |
| first_name_kana | string | null: false |
| birth_day | date | null: false |
Association
- has_many :products dependent: :destroy
- belongs_to :destination dependent: :destroy
- belongs_to :card dependent: :destroy
destinationテーブル
| Column | Type | Options |
|---|---|---|
| user_id | integer | null: false, foreign_key: true |
| family_name | string | null: false |
| first_name | string | null: false |
| family_name_kana | string | null: false |
| first_name_kane | string | null: false |
| post_code | string | null: false |
| prefecture | string | null: false |
| city | string | null: false |
| address | string | null: false |
| building_name | string | |
| phone_number | string |
Association
- belongs_to :user
cardテーブル
| Column | Type | Options |
|---|---|---|
| user_id | integer | null: false, foreign_key: true |
| customer_id | string | null: false |
| card_id | string | null: false |
Association
- belongs_to :user
categoryテーブル
| Column | Type | Options |
|---|---|---|
| name | string | null: false |
| ancestry | string |
Association
- has_many :products
※ancestryは、gem ancestryを使用するため。
productテーブル
| Column | Type | Options |
|---|---|---|
| name | string | null: false |
| price | string | null: false |
| description | string | null: false |
| status | string | null: false |
| size | string | null: false |
| shipping_cost | string | null: false |
| shipping_days | string | null: false |
| prefecture_id | string | null: false |
| judgment | string | |
| category_id | integer | null: false, foreign_key: true |
| brand_id | integer | null: false, foreign_key: true |
| shipping_id | integer | null: false, foreign_key: true |
| user_id | integer | null: false, foreign_key: true |
Association
-
belongs_to :user dependent: :destroy
-
belongs_to :category dependent: :destroy
-
belongs_to :brand dependent: :destroy
-
has_many :images dependent: :destroy
-
belongs_to_active_hash :prefecture
imageテーブル
| Column | Type | Options |
|---|---|---|
| image | string | null: false |
| product_id | integer | null: false, foreign_key: true |
Association
- belongs_to :product
brandテーブル
| Column | Type | Options |
|---|---|---|
| name | string | index: true |
Association
- has_many :products
ER図
これよりもコンパクトに作成できるよ!って方はぜひ教えてください![]()
以上、最後まで読んでいただきありがとうございました。