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

How to reload files in app/models/subdirectory in dev environment for STI

I implemented STI in one of my models, using some tips from Alex Reisner's blog post. I already had all my subclasses use the superclass's controller, with serialize/store to hold the extra attributes. I added the model_name and self.select_options…
3
votes
5 answers

Database design for 2 types of users

I have 2 ways for users to create an account on my website. a. Normal Registration Form (email, password) b. Registration via Facebook Connect (fb_userid, email) Which is the best practice to implement this using MySQL (InnoDB engine) ? My…
2
votes
1 answer

Nested Routing for Single Table Inheritance model rails 3.1

I created a Single table inheritance model in my model file and am having difficulty with the routing. When I use :as in my resource, it renames my named path. Model file: class Account < ActiveRecord::Base belongs_to :user end class…
2
votes
2 answers

Convert model in rails (Single Table Inheritance)

Let's say I have two types of users, A and B. Users of type B have fewer privileges and less-strict validations on fields (more can be blank). Otherwise, they're basically the same as type A. This fact makes me inclined to use single table…
Raphael
  • 1,701
  • 15
  • 26
2
votes
1 answer

DataMapper - Single Table Inheritance

Could Some one please explain to me what is going on here? This is an example that I put together to show y'all whats up: class Person include DataMapper::Resource property :id, Serial property :type, Discriminator property :name, String …
Don Wei
  • 431
  • 5
  • 16
2
votes
1 answer

Best ActiveRecord inheritance strategy for abstract class and subclass

trying to figure out the best ActiveRecord inheritance strategy for this particular problem: I have an abstract class, let's call it Message with the following methods/attributes. Message |- recipient |- sender \ body And I have two sub…
Tyler
  • 4,679
  • 12
  • 41
  • 60
2
votes
2 answers

Rails, STI and 'becomes' - f.object.errors not showing in view

My question is: why doesn't .becomes pass errors over to the new object? Isn't this the expected behaviour? I have the following single table inheritance classes in a rails app: class Document < ActiveRecord::Base validates :title, :presence =>…
Mike
  • 9,692
  • 6
  • 44
  • 61
2
votes
1 answer

Multiple DiscriminatorColumn in Hibernate inheritance mapping

I understood how to use DiscriminatorColumn in Hibernate inheritance mapping, However, in my scenario, I have more complicated inheritance module, where I need to define two Discriminators. I want to use one table for the entire inheritance…
stdcall
  • 27,613
  • 18
  • 81
  • 125
2
votes
3 answers

What are the merits of using STI vs Category?

This comes from the Rails world, but is a pretty generic question about Single Table Inheritance. They only reason I'm asking this is that people keep pushing STI on me when there is no obvious reason for it. May be I'm missing some point? Please…
konung
  • 6,908
  • 6
  • 54
  • 79
2
votes
1 answer

Hibernate Single_Table persistence The entity has no primary key attribute defined on subclass

Good evening. I'm trying to map a class hierarchy to a single table using JPA/Hibernate and receive an error on my subclass stating "The entity has no primary key attribute defined". The classes are defined as follows: @Entity @Table(name =…
2
votes
2 answers

How to get an instance of the base class in a Ruby on Rails class with inherited classes (STI)

I've got user and they've got pets. All kinds of pets. Reptiles, fish, birds, and mammals. The Mammal model has two sub-classes, inherited through single table inheritance (STI): cats and dogs. My user has a method to get their last pet. In a static…
2
votes
1 answer

STI and nested attribute mass assignment in mongoid?

Question on mass assignment through nested attributes using mongoid. Example: require 'mongoid' require 'mongo' class Company include Mongoid::Document has_many :workers,as: :workable, autosave: true accepts_nested_attributes_for…
2
votes
2 answers

Problem with single-table inheritance in edit form

I am using single table inheritance in ROR3 so I created these models: class Promotion < ActiveRecord::Base ... end class CreditPromotion < Promotion end class DebitPromotion < Promotion end Then I created a general form in a partial view <%=…
maxiperez
  • 1,470
  • 2
  • 20
  • 40
2
votes
2 answers

Sensio ParamConverter STI abstract

Given an Single Table Inheritance for Location -> A and Location -> B * @DiscriminatorMap({ * "a" = "A", * "b" = "B" * }) * @Discriminator(field = "discr", map = { * "a" = "A", * "b" = "B", * }) abstract class Location In…
2
votes
1 answer

Set up many to many associations between classes in single inheritance table and another table

I have a join table named Relations in a many to many relationship between departments and researchers. I want to be able to get a list of students by doing Department.find(1).students but I am getting…