1

In several different OOP languages I can make a class that inherits from another class. Can I do a similar things with models in databases or is that strictly not allowed? Is it possible to make model inherit from another model? I ask this thinking about a products class.

Generally speaking each product will have a name (nails, bolts, nuts), type (steel), some sort of size parameter and price. But could I maybe make an abstract model because different products may have different parameters that I care about, and then could I leave some columns as null?

Or do I need to declare all the different types of products and each product will have its own table?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sagick
  • 317
  • 2
  • 6
  • 20
  • 4
    Do NOT create a separate table in your database for each product. You create a product table. If products might have different unrelated attributes you can create a product/attribute table and then an attribute table to hold perhaps a key/value pair of the attribute. That gets you into EAV territory, but for this usecase it's not unheard of. Your question feels primarily like a Database Schema question, so this is definitely something you should invest some time in learning before concretizing this thing into a backend that will be hard to change in the future. – JNevill May 14 '19 at 20:43
  • It's not "strictly not allowed" - https://www.postgresql.org/docs/11/tutorial-inheritance.html - but it doesn't mean you _should_ use it in the design even if your DB supports it, as JNevill notes. – Nickolay May 15 '19 at 00:49

0 Answers0