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

SQL Party-Relationship Model - Consistency Between Sub and Super Class

In trying to solve a problem I was having with having different types of the same table, I decided to go with the Party Model design for relational databases - using multiple tables to represent the sub-objects. Using this post as well as this one…
funseiki
  • 9,167
  • 9
  • 36
  • 59
0
votes
1 answer

Rails User type selection in form for devise registration with Single Table Inheritance

I would like to make it possible for a User to select their "type" upon registration with devise. Do I need different registration forms for each "type" of User, or can I use one registration form? Everything is pretty basic: class User <…
0
votes
1 answer

What is the best practice to lazy load a field EF 5. 0 (CodeFirst)?

I have two main entities in my model. Report (also Report Table) and ReportFolder(also Table) [DataContract(IsReference = true)] public partial class Report { public Report() { this.DataSources = new…
Mojtaba
  • 1,210
  • 2
  • 12
  • 29
0
votes
1 answer

What is the correct SQL statement for has-many-through joins on multiple classes with single table inheritance

I have two tables, Places and Users -- Places Table: -- +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra …
bodacious
  • 6,608
  • 9
  • 45
  • 74
0
votes
1 answer

Understending STI and Polymorphic associations in rails

According to this answer i'm trying to inplement STI and Polymorphic associations together, my code: class Post < ActiveRecord::Base belongs_to :content, :polymorphic => true end class Topic < Post #ActiveRecord::Base has_one :post, :as =>…
Mykhailo Rybak
  • 163
  • 1
  • 2
  • 9
0
votes
1 answer

How can I add a foreign key constrain on a inherited table in postgresql?

I am using inherited tables in a project using posgresql. Something like that : create table root ( id bigserial, some_data text, ... ); create table leaf_a ( data2 text ) inherits(root); create table leaf_b ( maybe_other_data…
iXô
  • 1,133
  • 1
  • 16
  • 39
0
votes
2 answers

Relation between two single-table inheritance models

I have the following two models class ContactField < ActiveRecord::Base end class Address < ContactField end class Phone < ContactField end and class Contact < ActiveRecord::Base end class Company < Contact end class Person < Contact end I…
0
votes
1 answer

Doctrine recast entity as subclass

I'm updating a system which uses Doctrine2. It has a User class hierarchy which uses single table inheritance. Employee is the base class which is inherited by Department Admin and Company Admin. I'm trying to provide a way to promote/demote an…
John F
  • 865
  • 1
  • 8
  • 15
0
votes
0 answers

DataMapper and Single Table Inheritance

I have parent Person with two children Employee and Member, and Member has two children Regular and Premium. I have implemented the Single Table Inheritance approach in the database. now i would like to ask about the datamapper, how should it look…
Sisyphus
  • 900
  • 12
  • 32
0
votes
1 answer

Combining virtual attributes to single class attributes in super class (STI)

I'm trying to combine start_date, start hour, and start_minute virtual attributes from my Event form, in order to create a start_datetime attribute (which is stored in the database). I have (via STI) several subclasses of Event; let's call them…
0
votes
0 answers

Entity Framework Inheritance share database fields

So, I am using Entity Framework Code First. This is the model: public class StockMove { public int Id { get; set; } public int ProductId { get; set; } public virtual Product Product { get; set; } } public class StockMoveOut :…
0
votes
1 answer

Many to Many association with Single table inheritance

I am looking for a many-to-many association within the child models. Something like this below. Can you please guide what's the best way to do it. Parent class A < ActiveRecord::Base end Child1 class B < A has_many :bc has_many :c ,through:…
AnkitG
  • 6,438
  • 7
  • 44
  • 72
0
votes
1 answer

STI in rails with many-to-many relationship in the child classes

I have 2 models UnitPlate & PlantPlate which are a part of STI(Single table inheritance). I need many-to-many association with them. Is the below way of has_many: through and STI correct. I have the below models. Facing some issues in migrations…
AnkitG
  • 6,438
  • 7
  • 44
  • 72
0
votes
1 answer

Rails NoMethodError when calling method on parent

Playing around with Single Table Inheritance and hstore in Rails 4 but getting an error when trying to call method in the parent: # models/item.rb class Item < ActiveRecord::Base def getter_setter_for(key) define_method(key) do …
Meltemi
  • 37,979
  • 50
  • 195
  • 293
0
votes
1 answer

Single table inheritance the only viable option for tables with common attributes?

My goal is not to hack Yii in a totally different direction and it seems by following what I learned about inheritance and normalization that is not possible. On the image you can see what I would describe as an elegant solution for representing the…
Gregor Weber
  • 690
  • 9
  • 24