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
2
votes
1 answer

How to use discriminator value in Ebean queries when using single table inheritance?

I have the following entities with single table inheritance strategy. @Entity @Inheritance @DiscriminatorColumn(name="type") public abstract class Vehicle extends com.avaje.ebean.Model { @Id public Long id; public String name; public…
2
votes
1 answer

Hibernate ignoring discriminator column - always using 'dtype'

I have a strange situation in my SINGLE_TAB inheritance Hibernate config whereby the @DiscriminatorColumn seems to be ignored and the query is always defaulting back to the 'dtype' column. It's like the behaviour I would see when I had not included…
Ian
  • 73
  • 1
  • 4
2
votes
2 answers

ActiveRecord Problems using callbacks and STI

Hey folks, following problem with Rails and STI: I have following classes: class Account < AC::Base has_many :users end class User < AC::Base extend STI belongs_to :account class Standard < User before_save :some_callback end …
2
votes
2 answers

Rails ActiveRecord_Associations_CollectionProxy with Single Table Inheritance

I used Single Table Inheritance to model out Stores, Tailors, Orders, and TailorOrders. Orders belong to a tailor, but I am not able to access the orders of a tailor in the rails console. There are multiple types of stores, and in the database the…
2
votes
2 answers

Rails: cannot load fixtures for table using STI (undefined method `reflect_on_all_associations')

I can't load fixtures for my table that uses STI. When I call FIXTURES=schools rake db:fixtures:load, I get this error: undefined method `reflect_on_all_associations' for Object:Class Other fixtures load fine, i.e. FIXTURES=committes rake…
2
votes
2 answers

Adding additional fields on top of an inherited model and expose all super and child class fields

My Goal: I'm trying to create two different types of users, with different profiles. Barber, Client, BarberProfile, and ClientProfile I have my base User object that contains information like email, password, and all the other fields that Devise…
2
votes
2 answers

How does the :type field get populated in Single Table Inheritance?

I asked a question earlier about how to structure the data in a simple app I am building to manage content. The answer was to look at Single Table Inheritance and I think it's going to be the ticket. I've read quite a few examples but one thing…
jyoseph
  • 5,435
  • 9
  • 45
  • 64
2
votes
1 answer

Rails STI Association and nested resources

Ok, so I've got a weird pattern here that I can't figure out. I have an STI set with CallList as the base model, and City & State inherited. A city belongs to a state (and a state has many cities). A campaign has many call lists, so I want to…
2
votes
1 answer

Validation Failed when using accepts_nested_attributes_for with Single Table Inheritance

First time I'm using STI, and I'm running into issues when trying to use accepts_nested_attributes_for with nested inherited objects. class Document < ApplicationRecord # code removed for brevity end class DocumentItem < ApplicationRecord #…
2
votes
1 answer

Hibernate suitable inheritance strategy

This is the parent class @MappedSuperclass @Inheritance(strategy=InheritanceType.SINGLE_TABLE) public class APostCommon extends Actionable { private static final long serialVersionUID = 1L; @Column(name = "TITLE") private String…
2
votes
2 answers

ActiveRecord::SubclassNotFound error when using STI in Rails

I am using Ruby on Rails 2.3.10. I have a class, School. I use STI to give me several subclasses: PrimarySchool, SecondarySchool and University. app/models/primary_school.rb: class PrimarySchool < School has_many :education_records,…
Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63
2
votes
1 answer

rails HABTM to has many through

class QuestionSet has_and_belongs_to_many :questions, class_name: 'Exam', join_table: 'question_question_sets', foreign_key: 'question_set_id', …
2
votes
1 answer

Making a single create form using Single Table Inheritance in Rails

I'm using STI in Rails, and I've got a Vehicle object, that has many different types of subclasses, like Car, Truck, etc. It's for a simple app, so STI works fine in this case, but I'm having trouble creating a single form where any type of Vehicle…
joeellis
  • 2,745
  • 7
  • 28
  • 45
2
votes
0 answers

What are the possible solutions to OO/Table inheritance (ie. STI,MTI,CLI) in Rails 5+?

These are the options I see that can help solve "Rails 5 - Object Relation Impedence and how to structure multiple inherited classes/tables" TL;DR - the Object Table Impedance ORM problem. Abstract base class, with each child class having its own…
2
votes
2 answers

Rails has_many STI

I've trying to implement a STI on rails 4, but I can't make it work, I've search a lot of results but none worked. Here is the problem: I have an instance class, using STI I have a subclass Car (a dummy subclass) and ScheduledInstance class. class…