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

How to manage Single Table Inheritance within Doctrine 2?

I have comments and articles, both are votable. So, basically I've three entities, Article, Comment and Vote. After some reading on Single Table Inheritance in Doctrine2 reference manual, it seems that it's what I need, because my Vote remains the…
JohnT
  • 467
  • 6
  • 14
8
votes
1 answer

Single Controller, multiple (inherited) classes (rails 3)

I have a base class inherited by 2 others via Single Table Inheritance. I want all subclasses to share the same controller/views for various reasons-the only real difference is in the model's functionality. However, when I try to use link_to…
8
votes
2 answers

In Ruby on Rails, if we generated a model "Animal", and now want to have "Dog", how should we do it?

say, if we generated a model rails generate model animal name:string birthday:date and now we want to create other model to inherit from it (such as Dog and Cat), should we use rails generate model again or just add the files ourselves? How do we…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
8
votes
1 answer

How to group together multiple models under one name in Single Table Inheritance classes

(See below for link to sample project) WHAT I HAVE WORKING: I have many user types which I am handling using Single Table Inheritance in Rails, e.g.: class User < ActiveRecord::Base self.inheritance_column = :meta_type scope :doctors, -> {…
achabacha322
  • 651
  • 10
  • 32
8
votes
2 answers

"IllegalArgumentException occurred calling getter of" while running criteria with SINGLE_TABLE Inheritance strategy

I have this error while trying to list objects from database with hibernate Criteria decorated with simple Restrictions Criteria criteria = session.createCriteria(Licence.class); criteria.add(Restrictions.eq("gym", gym.getId())); List list…
Chris Jaga
  • 389
  • 4
  • 17
8
votes
2 answers

Doctrine Joining two Models with single-table inheritance, second join slows down

I have 3 models using Single Table Inheritance. They are for three different types of items that can be purchased on our website. The items are placed into categories, so the Category model has a property for mapping each of the three types. When…
Jessica
  • 7,075
  • 28
  • 39
8
votes
2 answers

Creating "feeds" from multiple, different Rails models

I'm working on an application that has a few different models (tickets, posts, reports, etc..). The data is different in each model and I want to create a "feed" from all those models that displays the 10 most recent entries across the board (a mix…
jsiarto
  • 93
  • 3
8
votes
3 answers

Spring: controller inheritance using @Controller annotation

I'd like to be able to create a base controller in my Spring app that, among other things, determines if a user is a registered user or not. This base controller, following the template design pattern, would contain an abstract protected method…
Rich Everhart
  • 1,043
  • 4
  • 19
  • 29
8
votes
1 answer

Single table inheritance scopes

Is it possible to set-up a scope on a single table inheritance that returns the subclass? For example: class Post < ActiveRecord::Base scope :sticky, -> { where(type: 'StickyPost') } end class StickyPost < Post end Now, when I call sticky on a…
Arjan
  • 6,264
  • 2
  • 26
  • 42
8
votes
2 answers

devise: instance the current_user using single table inheritance

I am using rails 3.0.9 and devise for authentication. Now I'm trying to use single table inheritance because I need to use polymorphism, so I have two classes: UserType1 and UserType2, which inherit from User class. I need that Devise instance…
7
votes
2 answers

single table inheritance with embeds_one mogoid

I have a model class Post include Mongoid::Document include Mongoid::Timestamps embeds_one :comment end and I have comment class class Comment include Mongoid::Document include Mongoid::Timestamps embedded_in :post field :title …
7
votes
1 answer

Is it possible to pluck from an includes association when using STI?

I have the following models using Single Table Inheritance (STI) in Rails 4: class User < ActiveRecord::Base has_many :notes end class Item < ActiveRecord::Base end #inherits from Item class Folder < Item belongs_to :folder end #inherits from…
7
votes
2 answers

counter_cache in single table inheritance

I am wondering if the counter_cache would work in single table inheritance. For these models: class User has_many :questions end class Question belongs_to :user, :counter_cache => true end class SimpleQuestion < Question end class…
7
votes
4 answers

Can nested attributes be used in combination with inheritance?

I have the following classes: Project Person Person > Developer Person > Manager In the Project model I have added the following statements: has_and_belongs_to_many :people accepts_nested_attributes_for :people And of course the appropriate…
7
votes
1 answer

SQLAlchemy: Single Table Inheritance, same column in childs

Im currently mapping a class hierarchy using the single table inheritance strategy (it is not possible for me to use joined). This hierarchy might look like this: class Parent(Base): __tablename__ = 'mytable' __mapper_args__ = { …
Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
1 2
3
40 41