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

What's the right way to make forms for creating/updating records with Single Table Inheritance (STI) in Rails?

If you have Single Table Inheritance in Rails something like this: class Vehicle < ActiveRecord::Base class Car < Vehicle class Truck < Vehicle What's the recommended way to create a single template which allows the user to create either a Car or a…
Eric
  • 935
  • 1
  • 8
  • 23
0
votes
1 answer

nested_form and Single table inheritance

I'm having trouble using it with STI and wondered if you can give me some pointers. I want to add attachments to discussion as well. I tried several attempts to make it work but always got exceptions. do i need to add accepts_nested_attributes_for…
Gady
  • 1,514
  • 2
  • 16
  • 32
0
votes
1 answer

Updating the instance of super class to an instance of sub class when using the single table strategy in hibernate

I am working on a project management system which has three users namely employee, Manager and HRM. The employee and Manager are in the same entity having many-to-many recursive relationship(let's call this Employee entity). The Employee entity and…
0
votes
1 answer

Postgres Replication on Inherited Table

I try to create a logical replication on a inherited table with Postgres 10. But if I create the subscription I get a error that the relation does not exist. Does anyone got a idea why this happen? The other way to inherited on a replicated table…
0
votes
0 answers

Reference same model twice with different foreign keys or create a many-to-many association

I have an app in Rails where I've create a model called User and another called Task. The model User has an enum for roles, which means that I can have an User that is a client or a developer: require 'bcrypt' class User < ApplicationRecord …
0
votes
1 answer

Rails STI Parent Model has_many expecting type on associated class?

Given a STI parent model class CustomForm < ApplicationRecord has_many :templates` end with a couple of subclasses that should have many templates. Why would the template class below: class FormTemplate < ApplicationRecord belongs_to…
0
votes
1 answer

Cast certain changeset fields into an ecto :map field

I have a changeset that looks like this: %{ creator_id: "4", name: "a test with GraphQL", place_id: "13", entree_id: "8", base: "wheat", type: "food" } My ecto schema is as follows: schema "items" do field :type, :string field…
S1r-Lanzelot
  • 2,206
  • 3
  • 31
  • 45
0
votes
0 answers

Cannot reference parent table primary key

Here is a simple example of my problem CREATE TABLE parent ( id SERIAL PRIMARY KEY ); CREATE TABLE child ( name text ) INHERITS (parent); -- Populating child table INSERT INTO child (name) VALUES ('Alex'); INSERT INTO child (name) VALUES…
urag
  • 1,228
  • 9
  • 28
0
votes
1 answer

Rails 4 - STI with namespace

If I have 3 classes like below: class Parent < ActiveRecord::Base end class Child < Parent end class Another::Child < ::Child end All those 3 classes are located in different folders. In rails console, Child.first run this query SELECT parents.*…
0
votes
0 answers

Sorting with hibernate single table inheritance strategy

In my application I have three entities, BaseNotification as a parent and SmsNotification and EmailNotification as child entities which extend BaseNotification. I am using hibernate single table inheritance strategy so all attributes fit into one…
alexv
  • 63
  • 1
  • 11
0
votes
1 answer

How should I model different document types?

I am building a Ruby On Rails API that helps manage construction documents -- there are a number of different types of documents that all have different fields, so I currently have a model for each. However, I also would like the ability to refer to…
0
votes
1 answer

Condition in SQLite trigger for inheritance

I'm working with SQLite,I am using XOR single table inheritance, I want to create a trigger that enables me to: Check before insertion if the InstructionRefs.id is already created in the table RideHeightRefs Ckeck before insertion that the…
Khaled
  • 81
  • 1
  • 14
0
votes
1 answer

inheritance foreign key rails

I am a bigginer, I have the following scenario in my rails' back end: I want to implement a parent class: Equipment: ID PK Brand IP ... And some child classes Printer: ID_EQUIPMENT PK and FK ... Computers: ID_EQUIPMENT PK and…
0
votes
1 answer

Rails single through table for multiple models

Whenever i try to add any of the user's role (say admin) and grade in user_grade table it is giving errors other roles must exist (teacher, student, guardian). I don't know how to fix this as this is bit complex relationship structure. or can anyone…
0
votes
2 answers

Rails Has_one and has_many relationship

I have Teacher model: class Teacher < User has_many :categories, dependent: :destroy has_many :grades, through: :categories end Grade model: class Grade < ApplicationRecord has_many :categories, dependent: :destroy has_many :teachers,…