Questions tagged [single-table-inheritance]

Single table inheritance is the simplest of several ways to design SQL tables that reflect a class/subclass or generalization/specialization relationship.

Single table inheritance is the simplest of several ways to design SQL tables that reflect a class/subclass or generalization/specialization relationship.

Inheritance is a central concept in object modeling, and is dealt with in class/subclass models. It is well understood by people who build object oriented systems. The parallel concept in ER (Entity-Relationship) modeling is called generalization/specialization. Unfortunately, many introductions to ER modeling do not present generalization/specialization, leaving the beginner to reinvent the concept on their own.

SQL, as such, has no mechanism for implementing inheritance. There are several design techniques for mimicking the effects of inheritance in SQL tables. The simplest of these techniques is called single table inheritance.

In single table inheritance, a single table is used to retain data that pertains to either the superclass or any of its subclasses. Each attribute will have its own column, and each instance will have its own row.

The result of this design is that all data about any member of the class can be obtained without doing any joins. If the intersection of a given column and a given row is not applicable, it is left as an SQL NULL.

SQL NULLS do result in slower retrieval of the rows that contain them, but this is generally offset by not having to do joins. SQL NULLS do increase the amount of space needed to store rows that contain them, but this is generally a secondary consideration.

Where NULLS can be problematic is when they appear in Boolean comparisons like equality tests in WHERE clauses. In SQL a boolean test can result in three possibilities: TRUE, FALSE, or UNKNOWN. If either side or both sides in a comparison are NULL, the result is UNKNOWN. If a test for equality yields UNKNOWN, the same test for inequality will also yield UNKNOWN. This can be baffling to people who are used to two valued logic. The use of nullable columns in WHERE clauses has to be considered very carefully, in order to avoid unexpected results.

Another issue is that it can be difficult to tell which subclass a given row belongs to. For this reason, a separate column, often called EntityType (e.g. VehicleType), is used to indicate subclass membership explicitly.

In complex situations, there are two alternatives to single table inheritance. One is called class table inheritance, which has its own tag: . another is called concrete table inheritance.

616 questions
5
votes
4 answers

Rails object inheritance with belongs_to

I have a simple has_many/belongs_to relationship between Report and Chart. The issue I'm having is that my Chart model is a parent that has children. So in my Report model I have class Report < ActiveRecord::Base has_many :charts end And my Chart…
Rabbott
  • 4,282
  • 1
  • 30
  • 53
5
votes
0 answers

Mongoid, Update parent and child at the same time

I have the following model structure: a Banner embeds_many Slides and each Slide embeds_many contents class Banner include Mongoid::Document embeds_many :slides accepts_nested_attributes_for :slides, allow_destroy: true end class Slide …
5
votes
1 answer

Is there a way to model table inheritance using MySQL Workbench?

Is there a way to model table inheritance using MySQL Workbench? I'd like the ERD to look similar to the left side of this image:
Matt Norris
  • 8,596
  • 14
  • 59
  • 90
5
votes
1 answer

Single Table Inheritance with Factory Girl in Rails

I'm making a Rails 4.0.1 app using Capybara and FactoryGirl but I'm having trouble getting my tests to work correctly. I'm using single table inheritance to make a Collection < ActiveRecord::Base and a VideoCollection < Collection model. When…
5
votes
2 answers

Doctrine Single Table Inheritance Query All Instances Of

I'm working on a notification system, so I have a notification abstract class and sub-classes (forumPostNotification, privateMessageNotification, etc). They are stored using Single Table Inheritance, so they're all in one table with a discriminating…
Jason Lin
  • 1,957
  • 3
  • 16
  • 18
5
votes
1 answer

Design of Rails app with multiple user types using Devise and STI

I'm relatively new to Rails, having written only one app so far. I used Devise for authentication in that app. Now I'm on to my second one, and I have to put more thought into authentication since I have many user types, as opposed to the single…
andreobrown
  • 156
  • 2
  • 10
5
votes
2 answers

How to model different users in Rails

Question I have a User model with authorisation and authentication logic built. Now I realise I have three different types of users. I want to store different information about each of them. What is the best way to handle this in Rails? Thoughts…
4
votes
1 answer

Ruby Class Inheritance: How to preven a public method from beeing overwritten in the child classes

Is it possible to prevent a public method from being overwritten in the child classes? class Parent def some_method #important stuff that should never be overwritten end end class Child < Parent def some_method #should not be…
4
votes
1 answer

Is there is any support for Single Table Inheritance in Prisma?

I have one project in Rails where I used Single Table Inheritance on Users Table, by creating two roles for Users table - 1.Clinician and 2.Patient. The model desc. is below class Patient < User has_many :clinician_patients has_many :clinicians,…
4
votes
1 answer

Rails: Structuring a query involving a polymorphic association and STI

I'm trying to find the 10 most recent comments on photos so I can integrate them into an activity feed on my Rails 3.0.3 application. I've got a Photo model, which inherits from an Upload model using single table inheritance: class Upload <…
4
votes
2 answers

JVM crashing when using any other Hibernate inheritance strategy besides SINGLE_TABLE

Ok, this is probably a longshot but here goes. In Java (JRE 1.6.0_26-b03) I have two classes, SuperControl and its subclass SubControl. They both need to be persistent objects and I'm using Hibernate Annotations to achieve this. I have approximately…
4
votes
1 answer

Handling forms with Single Table Inheritance

I have a form in my Profile edit view beginning with this line: <% form_for @profile, :html => { :multipart => true } do |f| %> Profile undergoes Single Table Inheritance and the two subclasses are Profile::Artist and Profile::Listener. When I try…
4
votes
1 answer

Doctrine 1.2 class table inheritance workaround?

ok, first of all, I know this is not possible with 1.2., that is why I'm looking for a workaround. And no, unfortunately I can't use Doctrine 2, because my shared hosting server is stuck at PHP 5.2.16, and the admin refuses to install PHP 5.3…
ZolaKt
  • 4,683
  • 8
  • 43
  • 66
4
votes
2 answers

Rails devise registration form when having STI

I dont know how to create a worker and a association. So i am able to link those together. I have a type colulm in user. This is my form(http://localhost:3000/workers/sign_up):

Create Worker

<%= form_for(resource, :as => resource_name,…
Rails beginner
  • 14,321
  • 35
  • 137
  • 257
4
votes
1 answer

Need classic mapper example for SqlAlchemy single table inheritance

I found an example of how to do single table inheritance using Class mappings. http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#single-table-inheritance But for the life of me, I cannot find an example of how to do this with classic mapper…