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

How to set up transitive persistence in single table inheritance with doctrine in zf2

I would like to set up transitive persistence in the single table inheritance mapping I’ve developed with doctrine in zf2. I’ve set up the mapping similar to the example presented in doctrine’s documentation for single table inheritance: /** *…
jcropp
  • 1,236
  • 2
  • 10
  • 29
2
votes
2 answers

Using a private method with Single Table Inheritance

I am working on a rails app where I have 2 different types of Users (MasterClientUser and AccountManager). I am using single table inheritance to differentiate the users. I have a update_last_seen_at private method that will need to be called on…
Steve_D
  • 545
  • 2
  • 11
  • 20
2
votes
2 answers

The state field path 'e.id' cannot be resolved to a valid type. Dynamic entity in orm.xml,

This simple SINGLE_TABLE inheritance hierarchy somehow fails when my application tries to access the inheriting entity RootFolderNode. The entities are defined as dynamic entities access="VIRTUAL", without a Java class backing. The PersistenceUnit…
leo
  • 3,528
  • 3
  • 20
  • 19
2
votes
2 answers

com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'DTYPE'

I am trying to make Single Table Inheritance into the Hibernate, but I am getting an error and I'm not able to figure it out. My inherited class @Entity @Table(name = "Vechicle") @Inheritance(strategy =…
2
votes
2 answers

Should I make 1 or 2 tables for Lecturers and Students in MySql?

I am currently working on a project where you save the details of a lecturer and student. I am not sure if I should use one table User or two tables Lecturer and Student. When you log in as a lecturer you have special privileges as its a group…
2
votes
2 answers

Hibernate inheritance table per class and single table mix

I am developing online web shop, and I need help with Hibernate mapping. I have the following inheritance: BaseProduct / \ Guitar Drum / \ / \ AcGuitar ElGuitar AcDrum ElectricDrum What I…
2
votes
1 answer

Rails Multi inheritance on database level

I would like to implement something along the lines of multi table inheritance for my rails application. I am familiar with how STI works and was wondering if the implementation would be similar. My situation is as follows (names of tables have…
2
votes
3 answers

Use `becomes` on a ActiveRecord::Relation (STI)

# Models class Animal < ActiveRecord::Base belongs_to :zoo end class Dog < Animal belongs_to :leg end class Snake < Animal end class Leg < ActiveRecord::Base end class Zoo has_many :animals end # Test, which fails with # Association named…
sudoremo
  • 2,274
  • 2
  • 22
  • 39
2
votes
1 answer

Is it OK to have multiple ActiveRecord `belongs_to` pointing to the same class?

In linking a sports event to two teams, at first this seemed to make sense: events - id:integer - integer:home_team_id - integer:away_team_id teams - integer:id - string:name However I am troubled by how I would link that up in the…
2
votes
2 answers

How to override the attr_protected?

I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car < Automobile end class Truck < Automobile end class User < ActiveRecord::Base has_many :automobiles accepts_nested_attributes_for :automobiles end I am…
Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
2
votes
0 answers

Rails has_many: through: association to a table that uses STI

I can't figure out how to make this work; I've looked at other SO questions including this one and this one but to no avail. Here are my models: class User < ActiveRecord::Base has_many :connections has_many :data_sources, through:…
2
votes
2 answers

How to structure this so I get all the benefits from STI with none of the consequences? (Pretty irresponsible, I know.)

Say I have the following example of associations in a Rails app: I'm considering combining the *Posting models under STI. One problem with STI is the potential for many attributes that are only related to one subclass (i.e., a lot of denormalized…
robertwbradford
  • 6,181
  • 9
  • 36
  • 61
2
votes
2 answers

Single Table Inheritance Alternative

I'm trying to think in a business model very similar to the one described here, using STI. class Person < ActiveRecord::Base # identified by email end class Owner < Person end class Customer < Person end class Employee < Person end class…
Marcelo
  • 1,702
  • 2
  • 24
  • 42
2
votes
2 answers

Propel Single Table Inheritance Issue

I have a table called "talk", which is defined as abstract in my schema.xml file. It generates 4 objects (1 per classkey): Comment, Rating, Review, Checkin It also generates TalkPeer, but I couldn't get it to generate the other 4 peers (CommentPeer,…
lo_fye
  • 6,790
  • 4
  • 33
  • 49
2
votes
1 answer

single table inheritance: must all classes in hierarchy have same properties?

I have the following class Item < ActiveRecord::Base end class Talk < Item end with the migration class CreateItems < ActiveRecord::Migration def self.up create_table :items do |t| t.string :type t.string :name t.text…
opsb
  • 29,325
  • 19
  • 89
  • 99