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

How do I mass assign from a form_tag param using STI

I am using STI to create a app which has a User parent and two children Employer and Jobseeker. The relationships have been setup and tested. I want to use STI to store Employer Users and Jobseeker Users into Postgres using only 1 users controller.…
Vishal Sakaria
  • 1,367
  • 1
  • 17
  • 28
0
votes
1 answer

Rails Field Validation Based on Value of Another Field

I need to validate that the value of a field (:discount) is one of an array of strings. The :discount field can also be blank UNLESS the :type field is 'FixedDeal' (STI) validates :discount, inclusion: {in: VALID_DISCOUNTS}, allow_blank: true unless…
user1264577
  • 124
  • 8
0
votes
1 answer

STI & Polymorphic association & user_id

So I have an assets model which is inherited by images and files as STI. Products has many images as assetable. This polymorphic association is working fine but I can't figure out how to add the user_id to every asset. A asset always belongs to a…
0
votes
1 answer

Is it possible to have a dynamic id column in Rails 3 when using single table inheritance

I have a Change model that utilizes single table inheritance that has the following attributes: id type #this is a single table inheritance type field. description dynamic_id I also have two sub classes, Race which is a subclasses of Change and…
0
votes
1 answer

Rails: Class inheritance and complex polymorphic has_many :through association

The app I'm developing has 3 main models and many single table inheritance models: Question User Professional Representant Taxonomy Category Topic Profession Locality Region Country There are multiple kinds of users (User, Professional < User,…
0
votes
1 answer

How to: Single Table Inheritance in DataMapper?

I'm learning Sinatra (1.3.2) and chose to use DataMapper (1.2.0) as ORM and an in-memory SQLite (1.3.6) DB to start. Two models, Books and Downloads, are sharing most attributes, so I looked into declaring a model for STI (Single Table Inheritance)…
0
votes
1 answer

Rails: Can a Nested STI Inherit Custom Methods

I have a resource that inherits its table and controller from its parent. Its parent also has custom routing that I want to pass to it, though I'm not sure how (or if it's even possible). The routes currently look like this: resources :publications…
nullnullnull
  • 8,039
  • 12
  • 55
  • 107
0
votes
1 answer

Optional type usage in Single Table Inheritance

i have an asset class which have type, name, label etc. Asset types are: avatar, image, video, summary etc. I want to have Video < Asset type only for type = 'video', but for all other types - Asset should be used. If i use self.inheritance_column =…
0
votes
1 answer

SQL UPDATE is not called in single Table inheritance if :type field is in form

I have a few models setup using STI Person < ActiveRecod..... Man Woman I've specified a type field and creating a new record works fine. The sub type (Man or Woman) is succesfully saved with the record and the in the type column is the…
0
votes
1 answer

complex find SQL using active record to associate records from two different tables with has_many relationship

I have been looking up the web to find a solution but to no avail. I am working on a problem where we have two tables in a has_many association, here are the model names class Employee < ActiveRecord::Base # main table stores all the employee…
0
votes
1 answer

Rails STI. How do I count the number of votes of type Upvote or Downvote for associated model?

Models are Vote and Issue. Issue has_many :votes. Vote has a STI column type that is filled with either :Upvote or :Downvote by a hidden form on the Issues index when a user votes on an issue. How do I find the number of votes of either type Upvote…
Dan
  • 641
  • 9
  • 25
0
votes
1 answer

Applying Single Table Inheritance

From that question: Summarizing: My CRUDs are: customers, employees and branches. Customers and Employees are associated with one Person, one Individual (fields related to the person itself) and one User (for login purposes). So Individuals and…
Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96
0
votes
1 answer

Branch conflict with single table inheritance

I wanted to play around with a single table inheritance approach in one of my tables. So i created a new git branch (for obvious reasons :) and created the STI for the table. Now when I switch back to my master branch for running some other tests,…
bpn
  • 3,082
  • 2
  • 19
  • 22
0
votes
1 answer

Single Table Inheritance with a legacy schema and composite keys

I'm trying to set up single table inheritance with a legacy schema but am having a hard time. Here is the schema for the table, bms_codes, being inherited from: create_table "bms_codes", :id => false, :force => true do |t| t.decimal "code_id", …
Ryan Arneson
  • 1,323
  • 3
  • 14
  • 25
0
votes
1 answer

How exactly does Rails prefills forms?

I have a (simple) question for my own curiosity: I'd like to find out how Rails prefill forms with posted values like... you know, when there's a validation error on some models' attributes then you do something like "render :edit" and the form is…
Kulgar
  • 1,855
  • 19
  • 26
1 2 3
40
41