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

Single table inheritance with relation targeting base class in TypeORM

In my app, I want user to have notification items (news) of different type. User and NewsItem must have one-to-many relationship, but also, NewsItem is just a base class for the different types of actual news items. Here's my base NewsItem…
Max Yankov
  • 12,551
  • 12
  • 67
  • 135
3
votes
1 answer

Query the database using discriminator values

I am using single table inheritance strategy. I want to perform a search in the database by filtering it using the discriminator type. How am I to write a function in JPA to perform this operation. The normal way of defining methods using findBy...…
3
votes
2 answers

Can model belong to STI child?

I have a base class Place and multiple sub-classes using STI conventions. I have a separate model Post, which belongs_to one of the sub-classes of Place: class Place < ApplicationRecord end class SubPlace < Place has_many :posts, class_name:…
Kobius
  • 674
  • 7
  • 28
3
votes
0 answers

Acttiverecord inverse_of when using single table inheritance

I have a system where an account can have many different types of order ( SalesOrder, PurchaseOrder, SalesInvoice, PurchaseInvoice, etc) I'm using single table inheritance and it works really well, even down to the level of controllers and views.…
John Small
  • 942
  • 2
  • 12
  • 21
3
votes
1 answer

Dynamically typed associations with STI models

I have a parent model Account with multiple subclasses using STI. I want to associate another model Transaction using a belongs_to relationship to Account. The referenced account could be either an Asset or a Liability. class Account <…
3
votes
3 answers

How to make Rails 3 reload STI classes in development mode?

After switching to Rails 3, I noticed that I have to reboot my server to make STI model classes reload with each request. For example, suppose I have this: # app/models/vehicle.rb class Vehicle < ActiveRecord::Base end # app/models/car.rb class Car…
rlkw1024
  • 6,455
  • 1
  • 36
  • 65
3
votes
2 answers

Single Inheritance or Polymorphic?

I'm programming a website that allows users to post classified ads with detailed fields for different types of items they are selling. However, I have a question about the best database schema. The site features many categories (eg. Cars,…
3
votes
1 answer

RSpec / FactoryGirl - Rails STI - equality

Simplified Example: I recently setup Single Table Inheritance on an Animal model. Cat and Dog are subclasses of Animal. I have an Animal factory : factory :animal do type { ["Dog","Cat"] }.sample end Almost everywhere in my test suite I…
3
votes
0 answers

PostgreSQL table inheritance and constraints

I have a DDL for some tables similar to the following: CREATE TABLE devices ( device_uuid uuid NOT NULL, manufacturer_uuid NOT NULL, ... CONSTRAINT device_manufacturer_uuid_fkey FOREIGN_KEY (manufacturer_uuid) …
filpa
  • 3,651
  • 8
  • 52
  • 91
3
votes
1 answer

rails redirect to the template of superclass if template is missing for some subclass

I am using Single-Table-Inheritance for a rails project. And I was wondering if it were possible to share functionality for some common functions between subclasses by implementing them in the superclass. Is it possible to recycle the views of the…
3
votes
3 answers

STI with Ploymorphic association in rails

I have model named Slider class Slider < ActiveRecord::Base end and HomeBannerSlider which has single table inheritance relation with slider class HomeBannerSlider < Slider has_many :images, as: :imageable accepts_nested_attributes_for…
3
votes
3 answers

Should I should subclass this Rails model?

I have a model called Coupon, which can either be set to have a money_off or percent_off attributes (it can only have one set at a time). Also depending on whether the Coupon is money_off or percent_off changes which methods are used. Im wondering…
Sam Mason
  • 1,037
  • 1
  • 8
  • 29
3
votes
3 answers

Should you use single table inheritance or multiple tables that are union-ed in a view?

Let's say you have a notes table. The note can be about a particular account, orderline or order. Notes that are about the account do not apply to any specific orderline or order. Notes that are about the orderline also apply to the parent order…
3
votes
1 answer

Mapping subclass list in Java using hibernate annotations

I have a problem with mapping a list of subclasses: Model situation - I have an abstract class: @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn( name="shapeType", …
3
votes
0 answers

Hibernate assign wrong class in SINGLE_TABLE inheritance strategy

I'm new to hibernate and I'm playing with inheritance strategies. I'm modeling scoring system for objects called Components. You can have TextScore and NumericScore in TextScoringCriterion and NumericScoringCriterion respectively. I decided to use…