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

DRY Routes for single controller Rails 4 STI Model

I have an STI model that has over 20 subclasses and I need to point them all to the parent controller in my routes. I have been defining each one in the routes, but this is really inefficient and not DRY. This is how it looks now. resources…
NothingToSeeHere
  • 2,253
  • 5
  • 25
  • 57
0
votes
2 answers

Multiple User Types with Devise and STI - Password Change Issue

I am building out an app using ruby 2.3.1 and rails 5, user authentication is Devise 4. This project, my instructor wants an app with User, AdminUser ClientUser and SupportUser types, he is not allowing DeviseInvitable to be used, this app will…
0
votes
1 answer

Multiple Devise Users: Multiple Models or Inheritance?

In my Rails application with Devise, I plan to make multiple types of Users (Student, Teacher, and Admin). They have a number of shared attributes such as username, email, password, etc, but with some differences. Students will be able to interact…
mc92
  • 475
  • 5
  • 10
0
votes
1 answer

How do we handle database schema with artwork types which unique has data for each type?

artwork.jpg I am trying to develop an artwork solution but stuck on the category type issue on how to code this into a database schema. I just started to understand the concept of parent and foreign key. An artwork belongs to a category type like…
0
votes
1 answer

How can I instantiate child objects using Rails' .build_ methods when using STI?

I have the following line in my create action: @financial_goal = current_user.send("build_#{type.underscore}",financial_goal_params) I'm having trouble with the build method not being defined, because I have not done a :has_one on my User model for…
RudyOnRails
  • 1,819
  • 3
  • 17
  • 26
0
votes
1 answer

single-table-per-hierarchy mapping error

I got the following mappings
jgauffin
  • 99,844
  • 45
  • 235
  • 372
0
votes
2 answers

Undefined Method 'underscore'

I'm trying my hand at STI using Thibault's tutorial: https://samurails.com/tutorial/single-table-inheritance-with-rails-4-part-3 It's been working fine up till the dynamic paths part where I get 'undefined method `underscore' for nil:NilClass' for…
0
votes
1 answer

Rails Nested create STI

I have 4 clases, with an STI on Instance. Workspace, Project, Task, Instance, (type1 < Instance) and (type2 < Instance). With proper associations. (Workspace has_many projects, has_many task through projects, so on) And I have this nested create…
Gaston
  • 1,004
  • 1
  • 9
  • 23
0
votes
2 answers

How to write unit tests for STI associations Ruby on Rails

What steps should you use when in a need to write unit tests for STI associations. I am completely confused. Please provide some suggestions or links to some tutorials. Thanks in advance
Rohit
  • 5,631
  • 4
  • 31
  • 59
0
votes
0 answers

Devise current_user with single table inheritance

I have a user model with an admin submodel. class Admin < User end I have devise set up and a 'type' column in the user model. The routes look like this: devise_for :users, :controllers => { :sessions => 'sessions' }, :skip =>…
user2759575
  • 553
  • 3
  • 25
0
votes
2 answers

ActiveRecord single table inheritance - how to get root model?

I have a model using single table inheritance and a concern that should work on any model. Consider this example: class Car acts_as_categorizable end class Suv < Car; end module Categorizable def after_safe siblings =…
0
votes
1 answer

Load entity by id with SINGLE_TABLE inheritance

I use Hibernate v.4 (inside Spring MVC). There are several inherited entities mapped on one table with SINGLE_TABLE strategy using DiscriminatorColumn: @Entity @DiscriminatorColumn @DiscriminatorOptions(force = true) @Inheritance(strategy =…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0
votes
0 answers

sti type from a dynamic form parameter in Rails 4

I am looking to pass an sti base class as a param and then be able to call all the child classes from that query. br/ This is my STI Class & Subclasses. class LivingThing < ActiveRecord::Base end class Animal < LivingThing end class Plant <…
NothingToSeeHere
  • 2,253
  • 5
  • 25
  • 57
0
votes
1 answer

Aggregation in Single Table Inheritance in Ruby on Rails

I'm working on Single Table Inheritance in Rails following this guide. The classes that I'm working with are as follows: Superclass: Articles Subclass: Tutorials, Projects, Thoughts All of them contain 'Comments' So I'm able to display all of…
Abundance
  • 1,963
  • 3
  • 24
  • 46
0
votes
2 answers

Rails/STI - common model path

I'm trying to learn how STI works and I have my Content model set up with Post and Article inheriting from it. I also have a Comment model. In the comments controller, I have this: def find_content @content =…
Raymond R
  • 1,350
  • 3
  • 12
  • 19