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

Struggling with complex Rails activerecord associations and join models

I am fairly new to Rails and I would really appreciate some pointers in the right direction. I understand the pros and cons of STI. What would be the best practices for modeling AR-relations with a combination of Single table inheritance and…
0
votes
0 answers

How to load initial data with playframework2.1 and ebean for single table inheritance?

I get this error: " NullPointerException: null No source available, here is the exception stack trace: ->java.lang.NullPointerException: com.avaje.ebean.Ebean.save(Ebean.java:585)" after clicking on "Apply this script now" on the initial page…
0
votes
2 answers

many_to_many, sti and will paginate

i have following code class Category < ActiveRecord::Base has_many :categorizations has_many :posts, :through => :categorizations end class Post < ActiveRecord::Base has_many :categorizations has_many :categories, :through =>…
0
votes
1 answer

Ruby on Rails single table inheritance sample form

i'm a super newbie in rails and i need to see a sample code on how to implement Single table iheritance, i have a model called Listing as a super class, and i have subclasses LawFirms and Paralegal, these all extend the Listing model, now i need to…
0
votes
1 answer

How to map entity property to entities of inheritance SINGLE_TABLE in hibernate?

I have entity hierarchy with inheriatnce type SINGLE_TABLE : @Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorValue("junioruser") @DiscriminatorColumn(name = "dtype", discriminatorType = DiscriminatorType.STRING) …
0
votes
1 answer

Scopes doesn't work with STI

I want to do STI in Rails. class AbstractUser < ActiveRecord::Base self.table_name = 'users' belongs_to :organization, :inverse_of => :users # reporter user has_many :requests, :dependent => :destroy # startup user has_many …
ciembor
  • 7,189
  • 13
  • 59
  • 100
0
votes
0 answers

In DataMapper, when using STI, what causes the error: undefined method 'key' for Object:Class

I'm getting a very strange error with DM 1.2 and a model that is sub-classed using single-table inheritance (STI): undefined method `key' for Object:Class That happens when I do a first query on the base model "User" (User.first) - however, queries…
mltsy
  • 6,598
  • 3
  • 38
  • 51
0
votes
2 answers

Rails STI: Forbidding parent class initialization

I have a rails model Book, with STI-inherited models Fiction and NonFiction While book holds a lot of common logic, I'd like to forbid creation of the parent Book model. Just wondering about the most elegant method for doing that in Rails - any…
PlankTon
  • 12,443
  • 16
  • 84
  • 153
0
votes
1 answer

Can't get an STI/ActiveRecord model to do what I expect

I'm trying to build a simple survey/questionnaire app. Surveys have Questions; most questions consist of a single content field (the question itself), for which the survey taker will write in a free-text response. (There are also a couple of other…
0
votes
1 answer

Rails 3 HasMany Through with STI

Here is my STI Models: class User < ActiveRecord::Base end class Athlete < User has_many :sports, :through => :user_sports has_many :user_sports end class Coach < User end The UserSports table has user_id and sport_id... but then you run…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
0
votes
2 answers

What is wrong in our Rails modeling mentality?

If Users --> students // employee (single table inheritence), and they both belong to a Organization --> School // Work (single table inheritance), what is the proper way to write the association? I put the organization_id into the User class, and…
0
votes
1 answer

Best way to insantiate a subclass that uses STI

I'm using single table inheritance in my app and I've run into a dilemma on how to best instantiate a subclass. Say we have a class Dwelling, and subclasses Apartment and House. Now Dwelling is associated with another class…
0
votes
1 answer

Rails STI on nested model

I have a question about STI in rails that I cannot seem to get my head around. I have 2 models, order.rb class Order < ActiveRecord::Base has_many :answers end and answer.rb class Answer < ActiveRecord::Base belongs_to :order …
nbon
  • 2,735
  • 2
  • 16
  • 15
0
votes
2 answers

Ruby on Rails: Possible/preferrable to implement single table inheritance after creating separate models?

I've built a simple address book application in my intro Ruby on Rails class with separate models for street addresses (Address), email addresses (Email), and web addresses (Web) using nested forms using a single controller (Entry). I'd like to now…
Richard D
  • 327
  • 3
  • 16
0
votes
0 answers

How do I inherit in a "second degree" within one table?

I have a class Resource0 and a class Resource1 which inherit from a class ResourceContainer1 which inherits from a class Resource. The model for my class Resource looks like this: class Resource < ActiveRecord::Base attr_accessible…
user2018381