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

Hierarchical database model

I'm designing a classifieds website about pets that will have 3 (main) categories of ads: A- Sale B- Breeding C- Jobs The problem I'm facing is that all 3 ad categories have some things in common, but they also have some differences. For example,…
0
votes
1 answer

Single table inheritance and Devise single Login

I am working on a Rails project using single table inheritance and devise. I have User, Client and Guide models. Client and Guide inherit from the User model. They have different paths for registration but I want them both to be able to log in on…
Macgill4444
  • 244
  • 1
  • 12
0
votes
1 answer

What is the steps for splitting childs table in postgresql?

I have a Postgresql database, where many tables inherit from a common root table. What are the SQL commands to use for removing the root table, but keeping data and final schema for each table? Thanks.
iXô
  • 1,133
  • 1
  • 16
  • 39
0
votes
1 answer

Getting STI type in polymorphic table

I have many types of vehicles, all having their respective brands. Each vehicle has one brand. And in this scenario below, I'm trying to figure out how to get .brandable_type to be equal to .type How do I return base_class with type…
0
votes
1 answer

Modeling a cellphone bill: should I use single-table inheritance or polymorphic associations?

In my domain: Users have many Bills Bills have many BillItems (and therefore Users have many BillItems through Bills) Every BillItem is one of: Call SMS (text message) MMS (multimedia message) Data Here are the properties of each individual…
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272
0
votes
2 answers

Adding STI to Existing Table

I want to add STI to an existing table using a custom type column. Let's call this taste_type whose corresponding model is Fruit. In the Fruit model I have: set_inheritance_column :taste_type In my migration to add STI I have: class AddSTI <…
keruilin
  • 16,782
  • 34
  • 108
  • 175
0
votes
1 answer

How to create a Select which let's you choose between valid types with single table inheritance in Ruby on Rails

i'm using Single Table Inheritance in ruby on rails to model various animals. So i have a base Animal Class and then i have various subclasses in the models/animal folder. When the user creates an animal, i want to show a select of the various…
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0
votes
1 answer

Rails: Refactoring for Single Table Inheritance to follow DRY principles?

I am creating a learning management system, where admins can create Curriculums and assign it to users. A Curriculum has_many Courses, and users mark courses as complete. Now, I want to extend this feature and add a Document model, where the admin…
kibaekr
  • 1,319
  • 1
  • 21
  • 38
0
votes
2 answers

Virgin STI Help

I am working on a horse racing application and I'm trying to utilize STI to model a horse's connections. A horse's connections is comprised of his owner, trainer and jockey. Over time, connections can change for a variety of reasons: The horse is…
Mutuelinvestor
  • 3,384
  • 10
  • 44
  • 75
0
votes
2 answers

Single Table Inheritance: Optional Plugin module and migrations

I want to know how you can solve this with ruby on rails: there is a core module which provides a class BasePlugin. Optional plugins inherits (single table inheritance) from this base class. Example: The FooPlugin from fooplugin module is a…
guettli
  • 25,042
  • 81
  • 346
  • 663
0
votes
1 answer

Extending a Doctrine entity to add extra association mapping in Symfony2

I currently have a User Doctrine entity and model in my Components. I have a User Bundle that does the basics of working with users, CRUD, etc. I am developing a resource allocation bundle and I want to extend my User entity to add extra…
Adam Sykes
  • 290
  • 1
  • 2
  • 14
0
votes
1 answer

How to get a mixed object types resultset from Doctrine2 Single Table Inheritance repository?

For the following schema: Animal - age - gender - size Cat extends Animal - fur_color Snake extends Animal - scales_color Elephant extends Animal - tusks_size When I do $em->getRepository('AcmeDemoBundle:Animal')->findAll() I will recieve a…
ioleo
  • 4,697
  • 6
  • 48
  • 60
0
votes
1 answer

rails validate uniqueness within inheritance

I have to models: Tag and TagNumeric each one with a category field I shouldn't be able to create Tags of different types with the same category. How can I validate this? EDIT: I forgot to mention TagNumeric < Tag class Tag include…
0
votes
1 answer

Single Table Inheritance with btn-group on create form

I've been trying to wrap my head around this single table inheritance example. I understand the concept behind it, but am not sure from reading other posts around the web & on this site how to make it work for my example. I'm using STI because…
0
votes
1 answer

SpringData fetch wrong class by JPA inheritance using SINGLE_TABLE

I've got a strange behaviour with Spring Data JPA and JPA class hierarchy using a single table. Basically I have two entities Vertrag and VertrieblerVertrag. VertrieblerVertrag extend Vertrag. The discriminator value is 'vertragsart' and it is 'KV'…