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

MySQL inheritance?

What would be the best way to design this MySQL database? I have cars, fuels, and motor oils. Each car can use any number of fuels and any number of motor oils. What would be the best way to design this database? So, one car can have one or many…
4
votes
2 answers

Dynamically eager-loading STI classes

Scenario: I have a number of STI models in my Rails 3.2 app. At times, I use parent classes to query the databases for child classes, as shown below: class ParentObject < ActiveRecord::Base end class ChildObject < ParentObject end class User <…
Henrique Zambon
  • 1,301
  • 1
  • 11
  • 20
4
votes
1 answer

Rails 3 STI load derived partial

Ok, so what I want to do, is have a template in a parent class, that loads a partial in the default template. That partial though should be specific to the derived class. def class DataItem < ActiveRecord::Base def value # do something fancy…
alex
  • 158
  • 6
3
votes
2 answers

Where do indexes go when using STI?

I am using Rails and postgres. I have a couple of models using STI and i was wondering where i should put indexes on the tables and why? For example lets say i have the following setup: class Comment < AR; end class MovieComment < Comment;…
3
votes
1 answer

Doctrine2 - Single Table Inheritance

I'm trying to apply this tutorial to my project, but I don't get it working. Everytime I try to update my schema i get an error: Fatal error: Cannot redeclare class Rueckgrat\App\Models\ProjectSetting in..... I have 2 files. The first one is…
Abenil
  • 1,048
  • 3
  • 12
  • 26
3
votes
2 answers

Polymorphism or Inheritance in JSON with Java and Ruby

For context, we are storing most of our data as JSON strings. This works very well with Hadoop on the backend and is easy to handle in Ruby on the front end. My data types fit the natural pattern for inheritance. For simplicity, lets say I have a…
Kevin Peterson
  • 7,189
  • 5
  • 36
  • 43
3
votes
1 answer

Updating an entity's type in TypeORM single table inheritance

Is there any way to change an inheriting entity's type (in the DB) to a different entity type?
3
votes
1 answer

Update entity type in TypeORM single table inheritance

Lets say I have a Base entity that implements typeorm TableInheritance (single table inheritance), and I have two deriving entities: A and B. I want to be able to change the entity type of A to B. something like this: const a = em.findOne(A, {}) //…
3
votes
1 answer

Getting right row while using inheritance of tables in PostgreSQL

I'm recently trying to drop Doctrine due to performance and problems with abstraction and move to database driven logic. I'm using mostly PostgreSQL. Doctrine One thing that I liked about Doctrine was inheritance, which I used for multiple…
3
votes
1 answer

Doctrine ORM Single Table Inheritance association problem (always Eager loading contrary to documentation))

I have an issue with single table inheritance and I'm not sure if I'm interpreting the documentation correctly. First: I've not copied my code / entity mappings verbosely (or even using the correct syntax) here as I think the problem can be better…
calumbrodie
  • 4,722
  • 5
  • 35
  • 63
3
votes
2 answers

Is this how django does Single Table Inheritance?

In this SO question I see the following: class MediaContent(models.Model): uploader = models.ForeignKey(User) title = models.CharField(max_length=100) created = models.DateTimeField(auto_now_add=True) def draw_item(self): …
Geo
  • 93,257
  • 117
  • 344
  • 520
3
votes
2 answers

How to convert entity leaving it id

There are some entities (Region, Country, City) which used STI (or even CTI). How it possible convert Country to City leaving old id?
Koc
  • 2,375
  • 2
  • 22
  • 26
3
votes
1 answer

Bug with Single Table Inheritance (STI) and has_and_belongs_to_many (HABTM) associations

Using Rails 3.0.7, and have the following 4 models: class User < ActiveRecord::Base end class Administrator < User has_and_belongs_to_many :clients end class Client < ActiveRecord::Base has_and_belongs_to_many :administrators …
3
votes
1 answer

Symfony, inherited entities and doctrine migration

Under Symfony 5.0, I use generic entity classes to unify internal projects. My generic entity (e.g. Table) looks like this: use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as…
3
votes
1 answer

Multiple models for one table Laravel

I have one problem. I try to create multiple models for one table in Laravel 5.6. I have a table for example Car, class for him: class Car extends Model { $fillable = ['type']; public function test(){ } } So, the field type is required. E.g.…