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
0 answers

Hierarchical Inheritance Support in ORMLite?

Below is my Class Structure @DatabaseTable(tableName = "Payment") Class Payment{ @DatabaseField(generatedId = true) public long id; @DatabaseField private long amount; @DatabaseField private String type; …
Sameer Khader
  • 157
  • 1
  • 5
0
votes
1 answer

Using a navigation property as discriminator in a TPH inheritance scenario in Entity Framework 4

I am trying to create a TPH inheritance hierarchy using foreign keys / navigation properties as discriminators and I am having some trouble getting it right. I have the following entities: Person: Id (int) Name (nvarchar) PlaneId (int) CarId…
Rune
  • 8,340
  • 3
  • 34
  • 47
0
votes
1 answer

Is this a good design in hibernate?

i have a parent entity Service and a child ExtendedService in a SINGLE_TABLE inheritance. A third entity ServiceCollector need to include both entites Service and ExtendedService. This is a fixed requirement, and with this design i can realize it…
blow
  • 12,811
  • 24
  • 75
  • 112
0
votes
1 answer

Is there a simpler way than using STI or polymorphic associations?

I have been reading a lot on the difference between STI and polymorphic associations and decided to use STI: user.rb Class User < ActiveRecord::Base has_many :articles end article.rb Class Article < ActiveRecord::Base belongs_to…
Timmy Von Heiss
  • 2,160
  • 17
  • 39
0
votes
2 answers

adding multiple profile types for users

I am designing a rails application where users sign in and create a profile. I wish for users to have a choice of 3 profile types (provider, seeker, professional). I have used the devise gem for user authentication. I have a profile model and have…
L.P
  • 19
  • 3
0
votes
1 answer

Using has_many with Multple Model Types via STI

How do I save and view a has_many relationship with 2 different models that are inheriting though STI? I've got a base Model for Projects as follows: class Project < ActiveRecord::Base attr_accessible :slug, :category_id, …
0
votes
1 answer

Parent/Child classes in Devise

I'm newish to Rails and working on a project that I would like to create a parent/child relationship using devise. I've looked through a good bit of the literature on Devise, but didn't see any clear cut way to do what I'm trying to accomplish…
PSCampbell
  • 858
  • 9
  • 27
0
votes
1 answer

ActiveRecord::SubclassNotFound, no matter what column name I choose

For my classes class User < ActiveRecord::Base self.inheritance_column = :user_type scope :customers, -> { where(user_type: '1') } scope :freight_forwarders, -> { where(user_type: '2') } end class FreightForwarder < User has_many :quotes,…
0
votes
1 answer

Multiple Devise models with unique attributes

Short explanation: I seek architectural advice and help in implementing multiple Devise models in a single app. More detailed explanation: My application needs to perform the following behavior: There are 3 types of users (Partner, Attendee,…
0
votes
1 answer

Many to many between categories and other models

Let's say we have two classes like so: class Book < ActiveRecord::Base end class Magazine < ActiveRecord::Base end These entities can have many categories, and each category can have many books or many magazines, but not both books and…
0
votes
3 answers

How does the Rails' single table inheritance works?

I have a user table, and a teacher that I newly created. The teacher is sub class of user, so, I use scaffold generator to generate the teacher table, than, I modify the model to do teacher is subclass of user. After all that, I did a db:migrate.…
Tattat
  • 15,548
  • 33
  • 87
  • 138
0
votes
1 answer

Rails STI: transfer records from one model to another

I have a model called Coupons Then I have two child models CouponApplicationsand ApprovedCoupons. The last two inherit from Couponsvia an STI architecture. Now I want to realise the following: A user sees CouponApplications He clicks on an Approve…
DonMB
  • 2,550
  • 3
  • 28
  • 59
0
votes
2 answers

Rails STI - ignore concern when child record created

My main model: class Coupon < ActiveRecord::Base include Concerns::CouponDistribution end The related Concern class: module Concerns::CouponDistribution extend ActiveSupport::Concern included do after_create…
DonMB
  • 2,550
  • 3
  • 28
  • 59
0
votes
2 answers

Rails Single Table Inheritance (STI): Use the same views for different models makes problems with form_for in 'new' action

I have a Boilerplate model which has two descending models: BoilerplateOriginal BoilerplateCopy While BoilerplateOriginals is sort of a template that admins create, BoilerplateCopys are copies of the originals that are free to edit by everyone,…
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152
0
votes
1 answer

Rails single table inheritance: type coumn is not populated automatically

I want to use STI in Rails 4. I already had a model Boilerplate and now I want to inherit a BoilerplateOriginal and a BoilerplateCopy model from it. So I added a column type to my table: class AddTypeToBoilerplates < ActiveRecord::Migration def…
Joshua Muheim
  • 12,617
  • 9
  • 76
  • 152