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

has_many :through method on an inherited class

BACKGROUND: I have a simple has_many :through setup linking Communities and Posts - which allows me to call @community.posts or @post.communities. I also have an Events model, which inherits from Posts OBJECTIVE: I'm wondering if there is a Rails…
0
votes
1 answer

Single Table Inheritance (STI) column associations

When using single table inheritance does one have to be careful not to populate the columns which are specific to different models? Is there a way to specify which columns each model uses?
Bryan Ward
  • 6,443
  • 8
  • 37
  • 48
0
votes
2 answers

rails 3 single table inheritance with multiple forms

I have set up a rails application that uses single table inheritance but I need to have a distinct form for my child classes. The application keeps a collection of indicators of security compromise, such as malicious IP addresses. So I have a…
Kevin Thompson
  • 2,466
  • 4
  • 29
  • 40
0
votes
1 answer

JPA Inheritance :mapping derived entities to different tables

I use SINGLE_TABLE inheritance startegy to map my usres (see code example bellow). Is there a way to map UnActiveRegularUser and UnActiveBusinessUser from "ACTIVE_USERS" table to another table, for example "UNACTIVE_USERS" and keep the inheritance…
0
votes
1 answer

Single Table Inheritance or Has Many Through in Rails

I think this is a simple question, but couldn't figure out my exact answer. I have an Event listing that has: People, Food, Costs, To-Do, etc. all as different information items. Instead of having to check each corresponding table if a related data…
dewyze
  • 979
  • 1
  • 7
  • 21
0
votes
2 answers

Would you introduce sql table inheritance overhead for these both tables

Would you introduce table inheritance for the TemplateTeststep and TestplanTeststep tables? The TestplanTeststep is/will be always a readonly copy of the TemplateTeststep (red) PLUS some editable fields (purple). The TemplateTeststep will have…
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
0
votes
2 answers

Rails Single Table Inheritance

For single table inheritance, how do you force Rails to use an integer column for the 'type' column instead of string?
Orion
  • 61
  • 1
  • 6
0
votes
1 answer

JPA Single Table Inheritance getType()

How do i retrieve the type parameter from parent class to determine which child class is being referenced. I have a simple example of Person table with discriminators Student, Teacher.. and i cannot find the getter for the type column? Is there in…
Warz
  • 7,386
  • 14
  • 68
  • 120
0
votes
1 answer

How would I setup a networked list of inherited classes in Rails

In my Rails app I'll have the following relationships: class Location < ActiveRecord::Base; end class Park < Location; end class Campground < Location; end class Trails < Location; end My goal is to be able to link all of specific types of locations…
jklina
  • 3,407
  • 27
  • 42
0
votes
1 answer

How to "extend" models with data in CakePHP properly?

I am rewriting an old hand-crafted-app of mine into CakePHP 2.0, and I bump into this old, ever-repeating Model design dilemma. This is the target design: Order has many OrderEntries OrderEntry has fields [amount, type] ProductEntry extends…
Vanja D.
  • 834
  • 13
  • 20
0
votes
1 answer

Rails Single Table Inheritance (STI) in a form

I have the following domain classes: class Rating < ActiveRecord::Base validates :kind_id, presence: true # Kind of rating (Class, Type, ...) end A rating specifies what class or type of aircraft a pilot can fly. If the pilot has a class rating,…
Laurens
  • 2,078
  • 5
  • 29
  • 46
0
votes
1 answer

Saving/updating a model with nested attributes and single table inheritance

I have a model, ModelRun, that accepts nested attributes for another model, ParameterValue. (ModelRun has_many :parameter_values.) However, ParameterValue also employs single-table inheritance to save two subclasses: NumericParameter and…
-1
votes
4 answers

Rails database setup Polymorphism

We have to create a request system which will have roughly 10 different types of requests. All of these requests will belong to the 'accounting' aspect of our application. Therefore we've called them "Accounting requests". All requests share maybe…
-1
votes
1 answer

Implementing STI where the parent class doesn't have a controller of its own its just a model and has a relationship with other models. Rails STI

I have the following scenario class XYZ < ActiveRecord::Base has_many :abcs end class ABC < ActiveRecord::Base belongs_to :xyz end class A < ABC end class B < ABC end class C < ABC end The model ABC doesn't have any controller, or view. Data…
Rohit
  • 5,631
  • 4
  • 31
  • 59
-2
votes
1 answer

Does single table inheritance results in denormalization

We're trying to come up with the data model of Payment Method. There can be several kinds of payment methods like Card, Bank Transfer, Wallet, which further can be categorized for e.g. Card into credit/debit cards, Bank Transfer into ACH/SEPA and…
1 2 3
40
41