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
4
votes
0 answers

Database design for rails app - attributes, polymorphism, or STI

I'm struggling with a database architecture decision for a Rails app. We have lots of different organizations and each organization has marketing assets they can sell. Examples of these marketing assets include: Banners Jerseys Web banners Email…
4
votes
2 answers

JPA Inheritance is not DRY

I've got inheritance working but it's not very DRY. Every new bolt type I have to repeat code. Best to show my classes then explain further My parent class for a BoltSpec (Dimensions pertaining to a…
jeff
  • 3,618
  • 9
  • 48
  • 101
4
votes
1 answer

Rails: Single Table Inheritance and models subdirectories

I have a card-game application which makes use of Single Table Inheritance. I have a class Card, and a database table cards with column type, and a number of subclasses of Card (including class Foo < Card and class Bar < Card, for the sake of…
Chowlett
  • 45,935
  • 20
  • 116
  • 150
4
votes
1 answer

Two models, one STI and a Validation

Let's say I have two tables -- Products and Orders. For the sake of simplicity assume that only one product can be purchased at a time so there is no join table like order_items. So the relationship is that Product has many orders, and Order…
keruilin
  • 16,782
  • 34
  • 108
  • 175
4
votes
2 answers

Rails single-table inheritance: determining class programmatically

In my Rails application, I am implementing a survey-like form which allows users to fill out answers to a dynamically changeable set of questions. Currently, I'm planning to support three different "types" of questions: Yes/No, Rating (on a 1-5…
Ajedi32
  • 45,670
  • 22
  • 127
  • 172
4
votes
1 answer

Peewee and Database Inheritance

I'm trying to learn Peewee and Bottle by making a book note-taking application. Say I have the following entities: Subject Book Chapter Note Tag I would like to be able to make Notes for Chapters, Books, and Subjects. In a DB, you would do: create…
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
4
votes
3 answers

Single Table Inheritance not working in Rails 4 app - ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed

I have a new Rails 4 app where I created a few models in the STI configuration. The main model is called Order and it inherits from ActiveRecord::Base. This is how it looks like: class Order < ActiveRecord::Base TYPES = %w(remote local) scope…
4
votes
5 answers

Single Table Inheritance (STI) parent ActiveRecord .subclasses .descendants returns empty

I have a STI in place for 10 models inheriting from one ActiveRecord::Base model. class Listing::Numeric < ActiveRecord::Base end class Listing::AverageDuration < Listing::Numeric end class Listing::TotalViews < Listing::Numeric end There are 10…
swapab
  • 2,402
  • 1
  • 27
  • 44
4
votes
1 answer

EF6 - TPH foreign key mapping in derived classes using base class property

I am using Entity Framework 6.0.2 with an existing database in which tags are stored in a single table that looks like this: Id: int, primary key TagType: string, determine the type of tag, either "usertag" or "movietag" ItemId: int, contains…
4
votes
7 answers

Single Table Inheritance

An answer to a question of mine on DB design suggested something called single table inheritance. I've done a bit of searching on that, but I can't seem to find that much clear information on it. Basically what I seem to understand from it is that…
robintw
  • 27,571
  • 51
  • 138
  • 205
4
votes
1 answer

Rails Nested Attributes / inverse_of and STI

I've been banging my head for a while on this one. The problem comes in when saving both a new referral (the parent object) and an appointment(child) at the same time. I've done similar with other nested objects but can't seem make it work with…
4
votes
2 answers

Doctrine extend Entity

I'm using Symfony with Doctrine. To resolve my problem I want to: or When I'm extending Entity class I want the doctrine to ignore parent's class @entity annotation (to see it as @MappedSuperclass) or (this one is more preferable) When I'm…
pleerock
  • 18,322
  • 16
  • 103
  • 128
4
votes
5 answers

Rails attr_accessible does not work for :type?

Im trying set the single table inheritance model type in a form. So i have a select menu for attribute :type and the values are the names of the STI subclasses. The problem is the error log keeps printing: WARNING: Can't mass-assign these protected…
LMH
  • 949
  • 9
  • 22
4
votes
2 answers

Hibernate trouble: INSERT instead of UPDATE when using Inheritance:SINGLE_TABLE and SecondaryTables

I have implemented inheritance hierarchy using SINGLE_TABLE with SecondaryTables. This works otherwise, but when my secondary table's field(s) are empty (= null in Oracle), next update to the entity fails since Hibernate thinks it should INSERT to…
4
votes
2 answers

Single Table Inheritance with model subdirectories in Rails 3.2.11

I have a model Sport. Using single table inheritance I have 2 other models, Cricket and Football so that class Cricket < Sport and class Football < Sport I put these two models in a subfolder inside models directory called sports. I added the type…