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
6
votes
3 answers

has_many and single table inheritance

I have a has_many relationship between two entities, Feeds and Posts. I also have specific types of posts, Videos and Photos. This is structured in the database using single table inheritance. Right now I have my Feed model specifying a has_many…
vrish88
  • 20,047
  • 8
  • 38
  • 56
6
votes
2 answers

Ruby On Rails Hierarchical Relationship Modeling

I have a base table called users which holds all common information about a user such as name, address, phone number...etc I have another table called clients which holds specific information about a client (such as the client's company name and…
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
6
votes
2 answers

is_a? fails with single-table inheritance in Rails 3

Object#is_a? is failing in the strangest way in Rails 3. I have single-table-inheritance set up as follows (simplified for brevity): # resource.rb class Resource < ActiveRecord::Base # blah blah end # video.rb class Video < Resource # blah…
rlkw1024
  • 6,455
  • 1
  • 36
  • 65
6
votes
2 answers

Rails STI using ONE form

I have a form that allows me to add files of different formats to a stream. So, a stream is made up of many files, these files are XML files but basically have different schemas. I have one form that allows the user to add whatever file they want, I…
Rabbott
  • 4,282
  • 1
  • 30
  • 53
6
votes
1 answer

has_many with multi-level hierarchy and single table inheritance

In my Rails app I have a multi-level hierarchy of the following kind: class Vehicle < ActiveRecord::Base end class RoadVehicle < Vehicle end class Car < RoadVehicle end class Buss < RoadVehicle end Then I have a class referencing the middle level…
Pascal Lindelauf
  • 4,782
  • 4
  • 40
  • 55
6
votes
1 answer

Can a single table inheritance entity extend a class table inheritance entity?

This is my base/parent entity, setup so its children are using their own tables. /** * @ORM\Entity * @ORM\Table(name="layer_object") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="discr", type="string") *…
6
votes
1 answer

Rails Single Table Inheritance: How to override the value written to the type field

In my system I have STI already defined. Dog inherits from Animal, in the animals table there's a type column with the value "Dog". Now I want to have SpecialDog inherit from dog, just to modify the behavior slightly in some special case. The data…
5
votes
1 answer

Pros and cons of Single Table Inheritance for Assets in Rails

I'm looking at file upload gems and there seems to be a tendency to put all assets in single "Assets" table and using STI to subclass them. Like ImageAsset, VideoAsset, AudioAsset, etc. I'm new to Rails and I've never used STI. Previously I would…
5
votes
3 answers

Can I remove the discriminator column in a Hibernate single table inheritance?

We use single table inheritance for every table in our application. This allows different instances of the same application stack to work with the same DAOs while their entities might differ slightly potentially containing information unique to that…
Pete
  • 10,720
  • 25
  • 94
  • 139
5
votes
1 answer

JPA OneToMany with inherited entities using DiscriminatorOptions and orphanremoval

I've a problem, that I can't solve for days now. I have read many docs, searched many forums, but found no solution. I've inherited class as the code shows below: @Entity @Inheritance(strategy =…
5
votes
3 answers

Ruby on Rails Single Table Inheritance (STI) and unit test problem (with PostgreSQL)

I'm using an STI model with a single "Accounts" table to hold information for Users and Technicians (i.e. User < Account, Technician < Account). Everything works from a functional perspective, but things explode when running unit tests: ... 8)…
5
votes
4 answers

Hibernate: org.hibernate.WrongClassException, SINGLE_TABLE inheritance and DiscriminatorFormula

I'm using Hibernate 3.2.2 GA with HSQLDB 2.0 GA, and I have a class hierarchy similar to the following: @Entity @Table(name = "A_TABLE") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorFormula(value = "case when CODE IN (1, 2, 3,…
Porcho
  • 51
  • 1
  • 1
  • 3
5
votes
2 answers

Broken Rails Routes after implementing Single Table Inheritance

I have implemented single table inheritance for a person class class Person < ActiveRecord::Base end class Teacher < Person end class Student < Person end class Outsider < Person end And the create person seems to work creating Teacher,…
Arc
  • 1,680
  • 6
  • 30
  • 57
5
votes
2 answers

Can a Discriminator Column be part of the Primary Key in Doctrine2?

I'm using Single Table Inheritance in Doctrine2 to store OAuth credentials for multiple services. I'd like to use the service's id as the primary key; however, that's not unique across all services. I've setup the database to use the discriminator…
Tim Lytle
  • 17,549
  • 10
  • 60
  • 91
5
votes
1 answer

How many classes is too many? Rails STI

I am working on a very large Rails application. We initially did not use much inheritance, but we have had some eye opening experiences from a consultant and are looking to refactor some of our models. We have the following pattern a lot in our…
Alan Peabody
  • 3,507
  • 1
  • 22
  • 26