Inheritance of individual tables

The answer to the question of my project, the DB suggested something called unidirectional inheritance. I have worked a little on this, but it seems that I can not find much clear information in it.

Basically, I understand that you have a large table with all the fields in it, as well as a type field, and then your ORM layer uses a type field to give you different kinds of objects. It is right?

More importantly, is unidirectional table inheritance an “approved” database design technology? By this, I mean, is it "reasonable" to use it? Is it safe to use or cause problems?

Another problem is how well does it work on rails? I found some links to it from the rails - but does it cause problems doing things in an unconventional way?

Any help is greatly appreciated.

+4
source share
7 answers

Is that a good idea? It depends. This violates normalization because the table does not have a single purpose. What happens when you extend the base class for the nth time? You will need to add columns to the table. Most modern databases have no problems with this, since you can change the table, but what to do with refactoring and deleting the class. You now have columns that have no purpose.

The rule of thumb is that if most of the design has been developed, it is probably safe to use. If the design changes frequently, you have other problems and you need to block your use cases / user requirements. (yes, not really XP)

I do not know about the effects of Rails.

+5
source

STI is a way to combat the mismatch between object-oriented and database-oriented thinking. This allows a reasonable representation of information in the database and another representation in the object model.

For example, I have an application in which each of my products contains one or more fees, each of which is calculated somewhat differently. In my object model, I want to have subclasses of the Fee class, each of which knows how to calculate itself. I really do not want to have a table for the type of board: therefore, I create a board as a base class and charges as a table that contains the union of all the fields necessary for all subtypes, plus the “type”, a column whose value corresponds to the name of the corresponding subclass. After that, ActiveRecord processes the plumbing.

+1
source

In short, single inheritance (STI) is a design pattern that allows you to map OOP inheritance relationships to a database. If you are defining any subclasses of objects in the ActiveRecord model, you should consider STI.

STI is (originally?) Documented in Martin Fowler’s book, "Models of Enterprise Application Architecture," and also described in DHH canonical Rails book "Agile Web Development with Rails" (section 18.4, I think.) I refer you to these books because they give a much better explanation than I could hope to do in this space.

I strongly disagree with the expression expressed at www.matthewpaulmoore.com (linked by robintw in this thread) that STI is inherently bad. This seems like a somewhat naive idea that there are discounts on using OOP inheritance. I used STI to create elegant solutions in Rails, but you can abuse any design pattern, I suppose.

+1
source

The final link . It allows one table to store several objects that have a common base class.

Ruby on Rails uses a library called Active Record . This is a common Ruby framework that supports STI.

0
source

I can only speak from the point of view of the (new) ADO Entity Framework, which includes table type functions (TPT).

Here 's a good series of blog posts that introduce basic concepts (using the Entity Framework), and it also has an MSDN article here .

There are also some recommendations when using nHibernate for TPT, it is located here .

Here is a link that explains type-based inheritance on SQL Server . This seems to be a pretty good summary and introduction to the concept.

I'm not sure what effect this will have on Rails.

0
source

All in all, I find this useful, but I ran into some bugs

An example in this link can give a useful picture of how you can use it.

0
source

Source: https://habr.com/ru/post/1277723/


All Articles