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

Trying to return various models with Ruby Activescaffold find on models with inheritance

class Promotion def self.get_todays_promotions # Promotion is a parent model, having child models e.g. # DiscountPromotion, VoucherPromotion, etc. # they all use a single table called promotions # (and having 'type' field…
jaycode
  • 2,926
  • 5
  • 35
  • 71
0
votes
1 answer

Creating a SQLite table row updating row again and again

I have created a table for my application... first time user will give the input for two editText ,name and mobile number when table is empty after that only updating the first row of table...so on
user3415586
  • 137
  • 1
  • 1
  • 8
0
votes
1 answer

Saving and retrieving inherited types with LINQtoSQL and Business Objects

I have an abstract EventBase class and some inherited event types, along with an Event class. Each event type has its own unique columns. In my data layer, I have a GetEvents method that simply does: from e in db.Events select new Event…
Dan
  • 724
  • 2
  • 9
  • 23
0
votes
1 answer

How to obtain action level protection using Authlogic and STI?

Given that it is well-documented how to use before_filter for a single user classification, I'm having trouble getting action-level protection for multiple user types. Let me explain: I've got something like this... class ApplicationController <…
Midwire
  • 1,090
  • 8
  • 25
0
votes
1 answer

linq to sql and inheritance question

Does anyone know how to to get the inheritance code for a specific type (single table inheritance in Linq to sql) ? When I create a new entity B (which is inherited from A), I would like to write some code in A, that is valid for all objects…
rekna
  • 5,313
  • 7
  • 45
  • 54
0
votes
1 answer

Converting Single Table Inheritence Models using ActiveRecord::Persistence#becomes

I'm using Single Table Inheritance in a Rails project and attempting to change the type of one model to that of another. Here are the relevant schema and models: create_table "images", force: true do |t| …
0
votes
1 answer

Model creation and association of STI subclass

I have an Event model that has one Payoption which can be of different types using STI subclasses like this: class Event < ActiveRecord::Base has_one :payoption, dependent: :destroy accepts_nested_attributes_for :payoption end class…
0
votes
1 answer

Rails single-table-inheritance vs. old data

I have a STI setup in Rails (names have been changed to protect the innocent) class Mercedes < Car class BMW < Car Now at some stage I need to remove references to Mercedes from the code, but want to leave the data in place for historical…
Chris Adams
  • 1,008
  • 1
  • 11
  • 22
0
votes
1 answer

How to prevent derived model from Single Table Inheritance in Rails 3.2.13 and how to convert inherited model object as derived model object

I've three models User, Group, Employee. Implemented Single Table Inheritance between User and Group model. But I don't want single table inheritance in Employee model which inherits User model but want to convert User model object as Employee model…
0
votes
1 answer

Setting up self-referencing HABTM relationship in Rails 4

I'm looking to set up a self-referencing has_and_belongs_to_many relationship using rails 4, with a postgres db. Basically I've got a Single Inheritance Table set up called Resource, which holds People, Places, and Things in it. (This works…
0
votes
1 answer

Fluent NHibernate inheritance mapping

In my hierarchy of animals Base one: public class AnimalMap : ClassMap { public AnimalMap() { Schema("dbo"); Table("Animals"); Id(x => x.Id).Column("ID").GeneratedBy.Identity(); Map(x =>…
Roar
  • 2,117
  • 4
  • 24
  • 39
0
votes
0 answers

How to search for DiscriminatorType in INNER JOIN FETCH-ed class in JPQL?

I have 3 java class with SINGLE_TABLE inheritance, as you see: The MessageTemplate parent entity: @Entity @Table(name = "message_template") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="MESSAGE_TYPE",…
victorio
  • 6,224
  • 24
  • 77
  • 113
0
votes
1 answer

Rails 4 routes with single table inheritance and self references

I've been jumping between design patterns, firstly trying polymorphic, now landing on STI. The main goal is to implement a Server > Host > Guest model where a Server has Hosts, Hosts have Guests and each able to have Posts. Although not the main…
0
votes
2 answers

How to make work single table inheritance and attr_accessible?

In a rails application, I have a base model like this : class User < ActiveRecord::Base attr_accessible :email, :name end And an inheritance from this model, like this : class Provider < User belongs_to :user attr_accessible :business_name,…
Dougui
  • 7,142
  • 7
  • 52
  • 87
0
votes
1 answer

Ruby on Rails: Subclasses

The idea behind the project: Content Management System. I have a superclass called Content, and a subclass of Text/HTML, for now. Later it will handle content types of Video and Images. All using Single Table Inheritance. My question is how to…
user2938958