Single Table Inheritance or Class Table Inheritance?

假装没事ソ 提交于 2019-12-30 06:03:30

问题


I'm reading about Class Table Inheritance (CTI) and finding I prefer it overall. The question I have is, is there any specific use case for Single Table Inheritance (STI) where you'd use that over CTI?

I read http://rhnh.net/2010/07/02/3-reasons-why-you-should-not-use-single-table-inheritance and as far as I know, it's solid. The use case for STI being a difference in behaviour but not data.


回答1:


I would like to point you to a great article I have found that explains with clarity why and when to use CTI. LINK




回答2:


Use STI for behavior differences like you said. An example I would use would be:

You have Purchase, and you have PartialPurchase, the only difference with data is that when a PartialPurchase is finished, it gets a relation to a newly created Purchase.

So the behavior is different, also, there are cases where I would want PartialPurchase and Purchase to show up in the same query. For a commercial agent, they want to see all of their purchases and partial purchases at the same time so it makes sense for this data to be in the same table. Otherwise, all of the attributes are the same for each model.

In that case I would use STI over CTI.

Though if ever the data started to differ a lot, I would probably create another table that relates to the STI table, and in the case of many differing fields, I would probably think of CTI.



来源:https://stackoverflow.com/questions/6073617/single-table-inheritance-or-class-table-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!