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

Is Single-Table Inheritance the right solution for my Rails problem?

Greetings, all, I'm working on an application in Ruby on Rails where we need to keep track of a bunch of external services for each user (for example, Facebook, MySpace, Google, SalesForce, Twitter, WordPress, etc) that the app will access on behalf…
2
votes
1 answer

Rails Single Table Inheritance using Foreign Key (ID)

I have to model an association structure and the association is divided into divisions/subdivisions/sections etc. So I've created a simple Entity-Attribute Model: I'd like to use rail's single-table-inheritance but it seems like this works only if…
sled
  • 14,525
  • 3
  • 42
  • 70
2
votes
3 answers

Rails STI query all subclasses using base class

Imagine I have this STI table called living things with these such subclasses: class LivingThing < ActiveRecord::Base end class Animal < LivingThing end class Plant < LivingThing end class Fungus < LivingThing end class Cat < Animal end class…
2
votes
1 answer

Why is ActiveRecord inserting NULL in the type column of an STI model?

I'm working on porting a very large Rails project from DataMapper to ActiveRecord.  Among the models that has to be ported is a set of User models that used Single Table Inheritance (STI) to distinguish one type from another.  Here is a simplified…
2
votes
0 answers

Using SINGLE_TABLE inheritance with Rest Repositories

I have the following classes: @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="vehicletype", discriminatorType = DiscriminatorType.STRING) public abstract class AbstractVehicle…
Rich
  • 15,602
  • 15
  • 79
  • 126
2
votes
2 answers

Using a Child's constant within a Parent's Validation

With the below code I am able to access the child's constant (ADDRESS_FIELDS) no problem within the initialize method (by using self.class::ADDRESS_FIELDS) but am unable to access it within the validation (getting NameError: uninitialized constant…
2
votes
2 answers

Single Table Inheritance in Rails and Devise options

If I have a main User model that has the standard devise line in it, and a Single Table Inheritance subclass model, how can I override my default devise options for the subclass? For example, the main user class has "confirmable", but I don't want…
V_H
  • 1,793
  • 3
  • 34
  • 59
2
votes
1 answer

Single table inheritance and changing type won't save

I have a STI model where the subclasses of each use different validations. The Rails defaults will run the original type's validations at runtime, so I am trying to use "becomes" to force the validations of the new type. My code looks like…
Dawn Green
  • 483
  • 4
  • 16
2
votes
2 answers

Error in Symfony 2 when create object of child entity class

I have a file /src/AppBundle/Entity/Questionnaire.php with 3 Entity classes, where I'm trying to implement Single table inheritance with Doctrine 2 on Symfony 2.7. Questionnaire is a parent abstract class, and there are 2 child classes…
Network_SPY
  • 263
  • 1
  • 2
  • 9
2
votes
1 answer

How do I order by a property that isn't a DB column using Doctrine?

When defining a relationship, there is a property on the related model (not a DB column), but I would like to sort by it (in the @OrderBy annotation). I have a base model that is extended using single table inheritance. The property in question is…
Sean the Bean
  • 5,222
  • 5
  • 38
  • 40
2
votes
1 answer

Single Table Inheritance & ActiveRecord Associations

I'm attempting to have a parent model that other models inherit from, and a secondary class that has associations with the children from the first model. Here's what I have so far... I used the first command to create the parent model. rails g…
2
votes
2 answers

How to build a select menu using STI types in rails with simple_form

I'm new to STI and I'm trying to build a select menu that lists all the STI types in a Model. I am using simple_form and Rails 4.1. How do I get a drop down menu with the StiRecord.types? I'd like to store the type in the database in a string. def…
2
votes
1 answer

EF inheritance with TPT - how to specify the foreign key column name?

I want to map two derived classes to two tables (TPT) [Table("User")] public class User { public int Id { get; set; } public string Name { get; set; } } [Table("Employee")] public class Employee : User { public int UserId { get; set; } …
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
2
votes
1 answer

Single Table Inheritance or Polymorphic?

So I have a database structure that looks something like this: Events: event_id: :integer event_type: :string name: :string slug: :string user_id: :integer start: :datetime finish: :datetime and Weddings which I need to inherit from…
2
votes
2 answers

PostgreSQL inheritance: get the record class name

I have a base table value_list with columns code, value, active. I have some tables vl_a, vl_b, vl_c, etc. inheriting from value_list. Is there a way, when doing a SELECT * FROM base to know from which class the child comes from. In other words, I…