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.

595 questions
175
votes
18 answers

Best practices to handle routes for STI subclasses in rails

My Rails views and controllers are littered with redirect_to, link_to, and form_for method calls. Sometimes link_to and redirect_to are explicit in the paths they're linking (e.g. link_to 'New Person', new_person_path), but many times the paths are…
ziggurism
  • 2,184
  • 2
  • 15
  • 20
82
votes
5 answers

Rails -- use type column without STI?

I want to use a column called type without invoking Single Table Inheritance (STI) - I just want type to be a normal column that holds a String. How can I do this without having Rails expecting me to have single table inheritance and throwing an…
Kvass
  • 7,654
  • 10
  • 59
  • 99
32
votes
6 answers

Single Table Inheritance in Django

Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate. Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes…
sutee
  • 11,322
  • 12
  • 46
  • 60
29
votes
3 answers

Changing type of ActiveRecord Class in Rails with Single Table Inheritance

I have two types of classes: BaseUser < ActiveRecord::Base and User < BaseUser which acts_as_authentic using Authlogic's authentication system. This inheritance is implemented using Single Table Inheritance If a new user registers, I register…
Wei
  • 397
  • 1
  • 5
  • 10
25
votes
4 answers

How to cast an ActiveRecord object to another class when using STI?

I'm currently using ActiveRecord single table inheritance. How can I cast one of my models from type A to B? They have the same parent.
fotanus
  • 18,299
  • 11
  • 71
  • 106
25
votes
6 answers

How to get the DiscriminatorValue at run time

We have the following classes @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) // optional annotation as this is default @DiscriminatorColumn(name = "apType", discriminatorType = DiscriminatorType.STRING, length =…
Shervin Asgari
  • 22,044
  • 28
  • 92
  • 138
19
votes
7 answers

How can I disable a validation and callbacks in a rails STI derived model?

Given a model class BaseModel < ActiveRecord::Base validates_presence_of :parent_id before_save :frobnicate_widgets end and a derived model (the underlying database table has a type field - this is simple rails STI) class DerivedModel <…
Orion Edwards
  • 113,829
  • 60
  • 223
  • 307
19
votes
6 answers

Twig instanceof for inheritance objects

I am using the following feature from propel http://www.propelorm.org/documentation/09-inheritance.html. I am also using Symfony2 and Twig I have a class structure using the above feature that looks something like this class Event {} class Birthday…
Alistair Prestidge
  • 659
  • 3
  • 9
  • 19
17
votes
2 answers

SQLAlchemy ORM: Polymorphic Single Table Inheritance, with fallback to parent class if "polymorphic_identity" is not found

Using Python 3.5 and SQLAlchemy 1.0.14 (ORM). I have a table of items declared as such: from sqlalchemy.ext.declarative.api import declarative_base Base = declarative_base() class Item(Base): __tablename__ = 'items' id = Column(Integer,…
Guillaume
  • 4,339
  • 2
  • 18
  • 38
16
votes
3 answers

Is it possible to create a conditional association in model?

I have setup a role based access controll system with the following models: Role (as STI), UserRole (global roles) ProjectRole (project specific roles) Assignment (Polymorphic with different resources) User Project (as one resource type for…
16
votes
1 answer

Hibernate HT_ Temporary Tables ON JOINED inheritance, Migration from Hibernate 3.4.0.GA To 5.1

I'm trying to migrate an application from Hibernate 3.4.0.GA to Hibernate 5.1, and after complete the required changes on java code, when I deploy the application I'm watching how Hibernate is trying to create HT_ tables (global temporary), one for…
karelss
  • 746
  • 1
  • 9
  • 24
15
votes
7 answers

Get a list/array of child classes from Single Table Inheritance in Rails?

I have a whole bunch of child classes that inherit from a parent class via single-table-inheritance in my Rails app. I'd like a way to get an array of all the child classes that inherit from the main class. I tried the following single-link command…
thoughtpunch
  • 1,857
  • 4
  • 24
  • 41
14
votes
6 answers

Rails STI: How to change mapping between class name & value of the 'type' column

Because of company rules I can't use our domain class names; I am going to use an analogy instead. I have a table called projects which has a column called 'type' with possible values as 'indoor' & 'outdoor'. Records having indoor and outdoor have…
arun
  • 143
  • 1
  • 1
  • 4
14
votes
4 answers

How to enforce referential integrity on Single Table Inheritance?

I've read some of Bill Karwin's answers about single table inheritance and think this approach would be good for the setup I am considering: Playlist -------- id AUTO_INCREMENT title TeamPlaylist ------------ id REFERENCES Playlist.id teamId…
13
votes
7 answers

How to run validations of sub-class in Single Table Inheritance?

In my application, I have a class called Budget. The budget can be of many types.. For instance, let's say that there are two budgets: FlatRateBudget and HourlyRateBudget. Both inherit from the class Budget. This is what I get so far: class Budget <…
1
2 3
39 40