テーブル継承パターン

E.g.

  • We have Note and Draft model class.
  • Parent model class of Note and Draft is Post.
  • And both have body and title field.
  • But Note only has published_date field.
  • Draft doesn't have it.

Concrete Table Inheritance pattern

Notes table

id: xxx
title:xxx
body: xxx
published_date: xxx, not null

Drafts table

id: xxx
title:xxx
body: xxx

Single Table Inheritance pattern

Posts Table

id: xxx
type: enum, note or draft
title:xxx
body: xxx
published_date: xxx, nullable

Class Table Inheritance pattern

Posts Table

id: xxx
title:xxx
body: xxx

Notes table

id: xxx
post_id: xxx
published_date: xxx, not null

Drafts table

id: xxx
post_id: xxx

Difference

PatternParent tableChildren table
Concrete Table Inheritance-x
Single Table Inheritancex-
Class Table Inheritancexx

NOTE:

  • This patterns are depended on premise of existence of multiple class.
  • But tables that will not be modified frequenly like a intermediate table for N:N is okay not to be separate.
  • That means it's okay not use this pattern.

References

  • https://en.wikipedia.org/wiki/Single_Table_Inheritance
  • https://github.com/bliki-ja/bliki-ja.github.io
  • https://www.martinfowler.com/eaaCatalog/singleTableInheritance.html
  • https://www.martinfowler.com/eaaCatalog/classTableInheritance.html
  • https://www.martinfowler.com/eaaCatalog/concreteTableInheritance.html