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

Multiple level of different kind of inheritance

For my project, I'm trying to use the inheritance feature of Doctrine. I need to represent medias (through different tables : one table for uploaded documents, one for linked videos, ... and so on). But, the videos can vary from provider to provider…
3
votes
1 answer

ActiveRecord Validations for Models with has_many, belongs_to associations and STI

I have four models: User Award Badge GameWeek The associations are as follows: User has many awards. Award belongs to user. Badge has many awards. Award belongs to badge. User has many game_weeks. GameWeek belongs to user. GameWeek has many…
keruilin
  • 16,782
  • 34
  • 108
  • 175
3
votes
1 answer

Accepting nested attributes on single table inheritance

I'm trying to get rid of duplication of strong params in controller that handles STI models. For example I have models: class Recipe < ActiveRecord::Base has many :fruits accepts_nested_attributes_for :fruits, allow_destroy :true end class…
3
votes
1 answer

How to use @UniqueConstraint with single table inheritance (JPA)?

I have a class extending an existing entity with single table strategy (which I can't change). I want to use UniqueConstraint for that entity so I tried: @Entity @Table(name = "t_document") public class Document implements Serializable…
3
votes
2 answers

Rails 4 update Type when migrating to Single Table Inheritance

Rails 4.0.4, Ruby 2.1.2 I want to use STI like so: User < ActiveRecord::Base Admin < User But currently I have: User < ActiveRecord::Base Info < ActiveRecord::Base So I changed my models, and then start writing my migration. In my migration, I…
Tom Prats
  • 7,364
  • 9
  • 47
  • 77
3
votes
2 answers

Rails single table inheritance/subclass find condition in parent

I have a table called Users (class User < ActiveRecord::Base) and a subclass/STI of it for Clients (class Client < User). Client "filtering" works as expected, in other words Client.find(:all) works to find all the clients. However, for users I need…
slythic
  • 311
  • 4
  • 15
3
votes
2 answers

Using STI path with same controller

I am using STI and am wondering, do I have to have a separate controller for each model? I have a situation where I only use the create and edit actions for one model in the STI relationship, but I get an 'undefined method' error if I try to do a…
TenJack
  • 1,594
  • 4
  • 21
  • 35
3
votes
2 answers

Remove rows from child table with Class Table Inheritance in Doctrine

I have made a really basic example with two models. "Singer" extends from "Person" I am using class table Inheritance with these two models:
manix
  • 14,537
  • 11
  • 70
  • 107
3
votes
1 answer

Rails Single Table Inheritance with HABTM Fixture in unit testing returning NoMethodError: undefined method `singularize'

Imagine a model structure as follows: models/cross_sell_promotion.rb class CrossSellPromotion < Promotion has_and_belongs_to_many :afflicted_products, :join_table => :promotion_afflicted_products, :foreign_key => 'promotion_id',…
3
votes
0 answers

RSPEC test Get/NEW custom STI routes

I have a Page class with a StaticPage subclass. I want to test that I can access a route for new static pages in RSPEC. routes: resources :sites do resources :pages, only: [:index, :destroy] resources :static_pages, except: :show,…
quantumpotato
  • 9,637
  • 14
  • 70
  • 146
3
votes
1 answer

Inheritance of rails models

I'm working on designing a genealogy rails application, and I'm having some trouble deciding how to design my models. Specifically, I'm working on things that will show up in a timeline. There will be many "things" that show up in the timeline. I've…
dkniffin
  • 1,295
  • 16
  • 25
3
votes
2 answers

ActiveAdmin Single Table Inheritance common attributes form rewrite

I'm using ActiveAdmin and I have a project with Single Table Inheritance. My question is simple: is possible to write only once the form of the common parts of my models or i am forced to rewrite it every time?
3
votes
1 answer

How to store different user's information in database?

My first post :) I just started learning Rails and have some doubts about database modeling. I am building a site for one soccer club and have many different user types. Each user can login to a site and have his own control panel. They also want…
3
votes
0 answers

Use both Class Table and Single Table inheritance in Castle Activerecord?

In Castle Activerecord (on top of NHibernate), is it possible to use class table inheritance globally, and single table inheritance on part of the inheritance tree? I would like to do something like /// /// Base class for models ///…
3
votes
0 answers

Rails nested form with a "select" form element giving me "undefined method `merge' for #"

Background: My goal is for a view to display a list of "condition" has_many objects, which are themselves STI subclasses of a StateDescription. I want a user to be able to pick what type of state description they are from a drop down menu (which…
J.R.
  • 5,789
  • 11
  • 55
  • 78