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

Rails Question: belongs_to with STI -- how do i do this correctly?

I've been playing around with STI and belongs_to / has_many relationships and I'm a bit confused. I have a few questions based on a model configuration similar to: class Parental < ActiveRecord::Base end class Mother < Parental has_many…
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
4 answers

Multiple Table Inheritance vs. Single Table Inheritance in Ruby on Rails

I have been struggling for the past few hours thinking about which route I should go. I have a Notification model. Up until now I have used a notification_type column to manage the types but I think it will be better to create separate classes for…
Tony
  • 18,776
  • 31
  • 129
  • 193
12
votes
4 answers

Rails: Using Devise with single table inheritance

I am having a problem getting Devise to work the way I'd like with single table inheritance. I have two different types of account organised as follows: class Account < ActiveRecord::Base devise :database_authenticatable, :registerable end class…
gjb
  • 6,237
  • 7
  • 43
  • 71
12
votes
1 answer

Change ActiveRecord::Base.inheritance_column in a rails app

I want to use Single Table Inheritance using a column other than type. According to the Rails documentation - http://api.rubyonrails.org/classes/ActiveRecord/Base.html, I can do this by modifying ActiveRecord::Base.inheritance_column. How can I do…
Eric Baldwin
  • 3,341
  • 10
  • 31
  • 71
12
votes
3 answers

How do I handle authentication with Devise when using multiple models in Rails 3.2 App

I'm working on a Rails 3.2 app where I use Devise for authentication. I decided to try single table inheritance for managing user roles, but I quickly ran into a problem. I currently have three User models, User < ActiveRecord, Admin < User and…
11
votes
2 answers

Single Table Inheritance or Class Table Inheritance?

I'm reading about Class Table Inheritance (CTI) and finding I prefer it overall. The question I have is, is there any specific use case for Single Table Inheritance (STI) where you'd use that over CTI? I read…
10
votes
3 answers

Hibernate: Is it possible to map multi-level inheritance to single table?

I have following inheritance hierarchy: Task | SpecificTask | VerySpecificTask And I'd like to persist it usign single-table inheritance, so I annotated…
mrzasa
  • 22,895
  • 11
  • 56
  • 94
10
votes
4 answers

Hibernate, single table inheritance and using field from superclass as discriminator column

I have following kinds of classes for hibernate entity hierarchy. I am trying to have two concrete sub classes Sub1Class and Sub2Class. They are separated by a discriminator column (field) that is defined in MappedSuperClass. There is a abstract…
Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
10
votes
2 answers

Single Table Inheritance and 'type' value for namespaced classes

While working on Rails 2.3.18 to Rails 3.2.x migration I am facing name issue in type column Here is the relation that is defined. app/models/reservation.rb class Reservation end class Reservation::Guest < Reservation end class Reservation::Event…
10
votes
5 answers

STI and polymorphs

I have problem with my code class Post < ActiveRecord::Base end class NewsArticle < Post has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at' end class Comment < ActiveRecord::Base belongs_to :commentable,…
10
votes
1 answer

Symfony2: Adding a collection based on a table-inheritance structure to a FormView

I am working on a Symfony2/Doctrine app which uses class-table-inheritance (http://docs.doctrine-project.org/en/2.0.x/reference/inheritance-mapping.html#class-table-inheritance) to manage Complaints in a Consult. Each Consult can have many…
9
votes
2 answers

Rails STI and multi-level inheritance queries

In my database I have a table people, and I'm using single table inheritance, with these classes: class Person < ActiveRecord::Base end class Member < Person end class Business < Member end The queries it generates confuse me. What I want is…
Obversity
  • 567
  • 2
  • 9
  • 21
9
votes
2 answers

Single Table Inheritance with Devise in Rails 4

I have read the posts here, here, and here, but I'm still having trouble with implementing Single Table Inheritance. Ideally I would like to have two registration paths (one for clients and one for providers) with the common fields name, email,…
9
votes
3 answers

Hibernate: Parent/Child relationship in a single-table

I hardly see any pointer on the following problem related to Hibernate. This pertains to implementing inheritance using a single database table with a parent-child relationship to itself. For example: CREATE TABLE Employee ( empId BIGINT NOT NULL…
Dchucks
  • 1,189
  • 5
  • 22
  • 48
1
2
3
40 41