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

Rails Model needs to validate includes? class_name

I have a model called ToolFilter with a column of 'tool_type'. The string here refers to a class for a tool. I put a method in my application_controller called tools_list that gets the descendants of Tool.This works nicely in my frontend, but…
0
votes
1 answer

Java SingleTable inheritance in db structure using JPA/Hibernate

I have models/Entities as follow: @Entity @DiscriminatorColumn(name="type") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) public class Insurance extends Generic { @OneToOne public…
masterdany88
  • 5,041
  • 11
  • 58
  • 132
0
votes
1 answer

Can I use Single Table Inheritance with has_many to get a collection of multiple types

I have a Citation class and I want each Citation to have many Resources. A resource can be a LocalFile, Link (and maybe something like a dropbox file). Resources will share most functionality and columns. I want to use single table inheritance in…
0
votes
2 answers

validate uniqueness amongst multiple subclasses with Single Table Inheritance

I have a Card model that has many CardSets and a CardSet model that has many Cards through a Membership model: class Card < ActiveRecord::Base has_many :memberships has_many :card_sets, :through => :memberships end class Membership <…
irkenInvader
  • 1,396
  • 1
  • 11
  • 17
0
votes
1 answer

Rails get data while using single table inheritance

I have company, indstry type, major category and minor category in my app. I used single table inheritance for major and minor categories. I have following association. class IndustryType < ActiveRecord::Base has_many :companies has_many…
vinothini
  • 2,606
  • 4
  • 27
  • 42
0
votes
1 answer

Getting datamapper to ignore 'type' when querying the database

I've got a single table inheritance structure in my application whereby Admins and Mods extends a User class. The table uses a discriminator value so each record has a type of either "admin" or "mod" When it comes to finding a user (on login) I'd…
Gerard
  • 4,818
  • 5
  • 51
  • 80
0
votes
1 answer

Joining two records with the same key

Let's say I have these two objects: public class Employee : Person { public string Something { get; set; } } public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set;…
0
votes
0 answers

EAV+Single Table Inheritance - Hydration Performance Issue - Doctrine2

Currently, I am experimenting with an EAV+Single Table Inheritance approach in Doctrine 2. I have a Base-Class that has many Values and I have multiple Sub-Classes of the Base-Class. The Sub-Classes are related. In the following CodeExample all…
0
votes
1 answer

Factory Girl tries to access a table that does not exist when using Single Table Inheritance

Here is my scenario: I want to store different types of events in my database. They all share the same fields but will behave differently. Therefore I used Single Table Inheritance. The table definition for event looks like this: class AddEvents <…
Sören Titze
  • 985
  • 1
  • 12
  • 31
0
votes
1 answer

Organizing a lot of models that use STI in rails

I have a scenario where I am going to be creating a large number of models that use STI and I'm wondering what the best way to organize this is. I already have other models using STI and I really do not want to add any more files to my models…
DavidP6
  • 307
  • 8
  • 19
0
votes
1 answer

Rails models with Single Table Inheritance and HABTM

I have a project with 2 models, Game and Team. A Game has two Teams, an away team and a home team. There is a set number of Teams(no more get created) and each will belong to many Games. I want to be able to do @game.home_team.name instead of…
0
votes
1 answer

Understanding Single Table Inheritance in JPA

I am new to JPA and doing a small sample to learn about it. But I got one problem below, please help me out, and please explain why: I have class Customer.java, which is mapped to table customer in db: @Entity public class Customer implements…
user2758327
  • 41
  • 1
  • 1
  • 4
0
votes
1 answer

Rails - How to create the right kind of object from Single Table Inheritance?

I've set up a Rails project to use Single Table Inheritance because I have two types of Users - Senders and Receivers. Senders have a public_key property and Receivers have a phone_number property. They share name, email, and password properties…
H K
  • 1,215
  • 2
  • 16
  • 29
0
votes
1 answer

using STI and ActiveRecordBase<> with full FindAll

Is it possible to use generic support with single table inheritance, and still be able to FindAll of the base class? As a bonus question, will I be able to use ActiveRecordLinqBase<> as well? I do love those queries. More detail: Say I have the…
oillio
  • 4,748
  • 5
  • 31
  • 37
0
votes
1 answer

Doctrine Single Table Inheritance and bidirectional OneToMany: columns not being generated

The data model: The entities: Pet: /** * @ORM\Entity * @ORM\Table(name="pet") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="pet_type", type="string") */ abstract class Pet { /** * @ORM\Id *…