单表继承还是类表继承?
我正在阅读有关类表继承(CTI)的内容,发现我总体上更喜欢它。我的问题是,单表继承 (STI) 是否有任何特定的用例,您可以在 CTI 上使用它?
我读了 http: //rhnh.net/2010/07/02/3-reasons-why-you-should-not-use-single-table-inheritance 和据我所知,它是坚固的。 STI 的用例是行为差异,而不是数据差异。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想向您推荐我发现的一篇很棒的文章,该文章清楚地解释了为什么以及何时使用 CTI。 链接
I would like to point you to a great article I have found that explains with clarity why and when to use CTI. LINK
正如您所说,使用性传播感染来解决行为差异。我将使用的一个示例是:
您有购买,并且有部分购买,与数据的唯一区别是,当部分购买完成时,它会获得与新创建的购买的关系。
因此,行为是不同的,而且,在某些情况下,我希望 PartialPurchase 和 Purchase 显示在同一查询中。对于商业代理来说,他们希望同时查看所有购买和部分购买,因此这些数据位于同一个表中是有意义的。否则,每个模型的所有属性都相同。
在这种情况下,我会使用 STI 而不是 CTI。
尽管如果数据开始出现很大差异,我可能会创建另一个与 STI 表相关的表,并且在有许多不同字段的情况下,我可能会想到 CTI。
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.