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

Grails "MappingException: Unknown entity" in inherited classes

I've got a handful of classes (simplified) in grails 2.5.5: class Solution implements Serializable { long id String name static hasOne = [ entity : SolutionEntity ] static mapping = { datasource 'companion' …
0
votes
1 answer

EclipseLink uses incorrect table name in queries for subclasses if inheritance type is SingleTable

I have three entities using JPA/EclipseLink: @Entity(name = "Sharing") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class AbstractSharingEntity extends AbstractEntity { @Entity(name = "InternalSharing") public class…
UoS
  • 81
  • 4
0
votes
2 answers

How to get a subset from the same entity as Listfield with hibernate annotations

I'm trying to represent a parent/child relationship between items who only differ through a single value in a special item_type column(with the value: NONE,PARENT,CHILD), Parents and Childs have the same "meta" article_id. I want to reuse the same…
0
votes
1 answer

One page model, different layouts/views. How can I leverage STI?

I have an app I'm building to learn rails. It's a simple page manager that uses awesome_nested_set to create nested pages. I would like to use STI but am unsure on how to make that work. Most of the examples I see of STI involve a main "Pets" model…
jyoseph
  • 5,435
  • 9
  • 45
  • 64
0
votes
1 answer

Rails Devise STI - undefined local variable or method `users' for

I'm using single table inheritance so my models Student and Teacher inherit from the same Devise model User (attributes are the same, only the relationships to other models are different). Now I was trying to display data from an instance of the…
megahra
  • 295
  • 2
  • 19
0
votes
3 answers

Rails Single Table Inheritance Devise helpers

I've created a User model with Devise and added a type column etc., so the models Student and Teacher could inherit from it. All of this worked great. My Teacher model has a one to many relationship to the model Course, where all the data about a…
megahra
  • 295
  • 2
  • 19
0
votes
1 answer

Select from two inherited tables in PostgreSQL

Say there is a PostgreSQL parent table files that is empty; it's 'abstract'. And then there are tables that inherit from files that contain data: red-files, green-files, blue-files. I can select from all three by selecting from files*, but how can…
jensph
  • 763
  • 1
  • 10
  • 22
0
votes
1 answer

Avoid custom route duplication for Single Table Inheritance

I have a couple of types of Users (Mover and Movee) created using Simple Table Inheritance. I am following a similar approach of passing in type in my routes as STI, one controller. But I find myself redefining the same custom routes in the base…
frank
  • 1,283
  • 1
  • 19
  • 39
0
votes
1 answer

Hibernate inheritance with SingleTable

I try to implement single table inheritance with hibernate 5.2 . Base Class @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="type", discriminatorType=…
0
votes
1 answer

Minitest: table empty when using STI

I had a single-table inheritance data model, where CityPage < Destination. This worked fine. Now that I have added a second type CountryPage < Destination, my tests fail. Specifically, CityPage.all returns an empty result even though I have CityPage…
henrebotha
  • 1,244
  • 2
  • 14
  • 36
0
votes
3 answers

Inherited table in SQL Server 2008. Performance issues?

I have this idea I've been mulling around in my head based on another concept I read somewhere. Basically you have a single "Primary" table with very few fields, other tables inherit that primary table through a foreign key. This much has been done…
instantmusic
  • 608
  • 1
  • 6
  • 18
0
votes
1 answer

Model with nested attribute creation changes association type attribute between before_validation and before_save

I have a model, Guest which takes nested attributes for Numberplate. Guest adopts STI (single table inheritance) and inherits from Person. To prevent the need for extra queries or joining to another table I save the person type in a field called…
edwardmp
  • 6,339
  • 5
  • 50
  • 77
0
votes
3 answers

rails: can't mass assign to these protected attributes while trying to set STI type during create

I have just started out learning rails and ruby, so please bear with me if this is too dumb. There are several different AppModule types in my app, which have different behavior but similar data, so I save them using single table inheritance.…
Engin Kurutepe
  • 6,719
  • 3
  • 34
  • 64
0
votes
0 answers

multiple user types with self references rails

I am working on an app in rails where I need to make multiple types of users. I am using Devise gem for authentication. Now let me give you an overview what I am trying to achieve and could not for past whole week. First, I want 2 types of users.…
Inzamam Tahir
  • 519
  • 4
  • 17
0
votes
1 answer

Doctrine single table inheritance - many-to-many assocication not working

I have different type of users containing different attributes, namely 1. Customer, fields=[name, email] 2. Professional, fields=[name, email, region, locations, categories, timeslots] 3. Admin, fields=[name, email] I expect few other type of…