LoginSignup
5
2

More than 3 years have passed since last update.

削除フラグ(is_deletedカラム)は作るべきではない

Last updated at Posted at 2020-02-13

削除フラグ(is_deletedカラム)の問題

以下のようなテーブルを想定する
アクティブuserと退会userがいるとしてis_deletedフラグで分けていた。
users

id name age is_deleted
1 太郎 20 false
2 花子 25 true

is_deletedカラムはエンティティの定義ではない

nameとageはuserの定義である。一方is_deletedは状態であるのでカラムの意味がnameとageと異なってしまう。
アクティブuserと退会userと定義が異なるユーザーが混在してしまう。

is_deletedカラムだけで表現できない場合がある

途中から休止userを作ることになったと仮定する。
するとis_deletedだけでは表現できなくなってしまった。
その場合途中からテーブルを変更するのがとても大変である。

どうやって解決するか?

users

id name age
1 太郎 20
2 花子 25

users_active

user_id
1

users_deleted

user_id
2

上記のようにテーブルを分けることでカラムの意味を混ぜることがなくなった。
そして定義が異なるユーザーを分けることができた。
また新しい状態のuserが出てきてもテーブルを追加するだけでよい。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2