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

Rails single-table inheritance relationships

I have a teacher model which is inherited from user model and a separate class model. Now I need to set up many_to_many relationship between teacher model and class model. How can i do this, I'm little confused?
0
votes
1 answer

Rails: Single Table Inheritance - Subclass model with specific attribute

Lets say I have a model named Animal with attribute species. I intend to subclass Tiger from Animal, which attribute species is "tiger", how to define it so that Animal.where(:species => 'tiger').where(:age => 12) can be written as Tiger.where(:age…
0
votes
0 answers

Have a derived table with records filtered by property of base table

In my case, I have two classes. One base class and one derived class. This is my base class: public class A { public long Id {get; set;} public bool Condition {get; set;} } and here is my derived class: public class B : A { public bool…
0
votes
1 answer

Hibernate inheritance single table

I am trying to implement a single table Hibernate inheritance strategy with a DB table "persons". I have a family which contains a husband, a wife, a list of children and a list of other inmates. Each person may be a member of many families (i.e. a…
Gandalf
  • 155
  • 1
  • 12
0
votes
1 answer

RoR STI Single of each type for every parent model instance

Im trying to validate the count of models that inherit from Parent model and keep them unique. So i have User has_many :parents Parent belongs_to :user Also I'm using single table inheritance for this , so: rails g model Type1Parent…
frcake
  • 229
  • 1
  • 3
  • 18
0
votes
0 answers

Single Table Inheritance and has_many_and_belongs_to

I use single table inheritance like this: class Reservation < ApplicationRecord end class Stay < Reservation end Now I've got another class called room and I want it to be in many-to-many relation with the stay because reservation is more general.…
Gregg
  • 125
  • 1
  • 2
  • 11
0
votes
1 answer

Heroku STI Problem

This is my first time working with Single Table Inheritance, and it has been a great experience until pushing the app to Heroku. I have a User model (and table with a type column), a Hauler model that inherits from User class, and a Generator model…
aguynamedloren
  • 2,273
  • 18
  • 23
0
votes
1 answer

Single Table Inheritance Errors - ActiveRecord::SubclassNotFound

My intent is to implement STI with two types: Staff and Clinician. My previous implementation was using roles with enums, and after doing my best to follow answers to similar questions, take out all references in tests etc. to enum roles and replace…
mike9182
  • 269
  • 1
  • 3
  • 17
0
votes
2 answers

rails before_create not called when creating an object with Object.const_get().new

I have a single table inheritance mechanism and a controller method that creates objects based on a text type. From my controller: tile = Object.const_get(tile_data[:type]).new(params_from_tile(tile_data)) tile.save inside my model base class I…
acolchagoff
  • 1,926
  • 3
  • 18
  • 30
0
votes
2 answers

In a STI table/modeling schema, how can one have type specific attributes that are shared by all types?

I have a set up where I have multiple models inheriting from a Base model - standard Single Table Inheritance: class Parent < ActiveRecord::Base end class A < Parent end class B < Parent end My STI setup is correct and works great! However, I…
0
votes
0 answers

use type column without STI in rails

I am working on the scenario which is different and I have model structures like This is my main model which has STI and working fine. class Post < ActiveRecord::Base # table is "posts" as per default behaviour # have column "type" end This is…
0
votes
2 answers

Rails 5.1.2 - Single Table Inheritance: No migration is getting generated

I am trying to generate scaffolding for STI implementation. I issue the following. rails g scaffold user1 type name email rails g scaffold member company subscription --parent user1 Every thing gets generated file except for the migration file my…
0
votes
1 answer

Rails single table inheritance validation

There is a Request model in my app. On different pages I need different validations, for example on /contacts I need to validate a lot of fields, whereas in a 'call me back later' popup I need to validate only phone number and name. My problem is:…
0
votes
1 answer

How to generate a map in java 8 from a list in hibernate single table strategy for the below scenario

@Entity @Table(name="PLAYER") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn( name="TEAM", discriminatorType=DiscriminatorType.STRING ) public abstract class Player{ @Id …
pbk
  • 11
  • 1
  • 5
0
votes
0 answers

Use Single table inheritance in Django

AFAIK Django supports these types of inheritance: Abstract base classes Multi-table inheritance Proxy models According to the StackOverflow tag description, Single table inheritance is the simplest of several ways to design SQL tables that reflect…
guettli
  • 25,042
  • 81
  • 346
  • 663