Questions tagged [class-table-inheritance]

Class Table Inheritance is one of several techniques for designing SQL tables in situations where subclasses that extend classes would apply if SQL had a mechanism for inheritance, which it doesn't.

SQL as such has no formal mechanism for implementing inheritance on behalf of the database builder. However, there are ways to design tables that mimic, to some extent, the behavior you would get from subclasses that extend classes in an object oriented environment.

Class Table Inheritance is one such design technique. In this technique, there is one table for the class, and one table for each distinct subclass. Columns that are relevant to all members of the class go in the class table. Columns that are only relevant to some subclasses (often only one subclass) go in the appropriate subclass table(s). The concept can easily be extended to cases where the class is itself a subclass of some even more generic superclass.

The primary key of the class table and the primary key of the subclass tables are usually shared. This is described under the tag . Implementing shared primary key involves some work on the part of the programmer, because the primary key has to be propagated from the class table to the appropriate subclass table(s) by programmed action whenever new entries are made in the class table. Shared primary keys enforce the one-to-one nature of the relationship.

A join between one of the subclass tables and the class table is simple, easy, and fast. All rows in the class table that do not pertain to the subclass at hand will drop out of the join. For convenience, these joins might be kept in defined views.

There are circumstances where the subclasses are mutually exclusive. A pet is never both a dog and a cat. There are cases where the subclasses are not mutually exclusive. One person might be both a student and an instructor at a university.

A different but simpler technique for dealing with inheritance is described under the tag .

105 questions
6
votes
1 answer

Can a single table inheritance entity extend a class table inheritance entity?

This is my base/parent entity, setup so its children are using their own tables. /** * @ORM\Entity * @ORM\Table(name="layer_object") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="discr", type="string") *…
5
votes
2 answers

understanding class table inheritance

I have a product table called products It has 3 fields (name,model(PK), and class_name). The class corresponds to a table. So here is an example: Product table: model | name | class_Name z123 | Abcd | AMPS AMPS table: model | attribute_1 |…
Ryan
  • 14,392
  • 8
  • 62
  • 102
5
votes
1 answer

Pros and cons of Single Table Inheritance for Assets in Rails

I'm looking at file upload gems and there seems to be a tendency to put all assets in single "Assets" table and using STI to subclass them. Like ImageAsset, VideoAsset, AudioAsset, etc. I'm new to Rails and I've never used STI. Previously I would…
5
votes
2 answers

Doctrine 2.1 - Map entity to multiple tables

I have the following database situation: wp_users (user table generated by wordpress) ID | user_login | ... wp_sp_user (extension to the wp_users table) ID (FK) | surname | address | ... Now I've already been trying for hours to "fuse" those two…
MrMuh
  • 319
  • 1
  • 4
  • 13
5
votes
2 answers

Multiple table inheritance Rails and activerecord

I'm trying to implement Multiple Table Inheritance with ActiveRecord. Looks like all the available gems are pretty old. Am I missing something? Is there any "native" way to implement this with activerecord? I'm using Rails 3.2.3 and activerecord…
4
votes
1 answer

Determine type from Class Table Inheritance

I am trying to implement a database that has a table structure similar to below, where 2 tables are a subtype of a table. An animal has a primary key, and dog and cat has a foreign key that references animal_id of animal. animal(animal_id,…
zychin
  • 65
  • 5
4
votes
1 answer

Doctrine class-table inheritance property has wrong value on query

I use two concepts of Doctrine in my model : class-table inheritance Lifecycle callbacks When I load a inherited instance of my parent class, the parent property which is updated with the lifecycle callback is wrong, well, not the value I have in…
4
votes
1 answer

SQL Concrete Vs Class Table Inheritance query speed on one subtype

I am weighing up between Concrete and Class Table Inheritance (see example below). Class Table certainly has a lot of benefits, in particular for my scenario, super table columns are guaranteed consistent across the full data set. However I have…
4
votes
1 answer

Doctrine Class Table Inheritance - Eager loading sub-class references

Consider the following diagram: Where a User has many BlogPost's, an Event is a type of BlogPost with a property of Venue. I have the following entity classes: ** * @ORM\Entity() * @ORM\Table(name="User") */ class User { ... /** *…
Rob Forrest
  • 7,329
  • 7
  • 52
  • 69
4
votes
1 answer

Can the ClassTableInheritance plugin for Sequel be configured to store something other than the model name as the key?

I am using the class_table_inheritance Sequel plugin for my project and I have the following models: class Account < Sequel::Model plugin :class_table_inheritance end class TwitterAccount < Account; end class FacebookAccount < Account; end class…
Reid Main
  • 3,394
  • 3
  • 25
  • 42
4
votes
1 answer

Error : Cannot use identity column key generation with ( TABLE_PER_CLASS )

I am using Hibernate to save entities where all entities are inherited from a base abstract entity. For all concrete Entities there are database tables respectively, with autoincrement primary key column. But using GenerationType as "AUTO" or…
Ganapat
  • 6,984
  • 3
  • 24
  • 38
4
votes
2 answers

MySQL inheritance?

What would be the best way to design this MySQL database? I have cars, fuels, and motor oils. Each car can use any number of fuels and any number of motor oils. What would be the best way to design this database? So, one car can have one or many…
3
votes
1 answer

How to specify foreign key column for Class Table Inheritance in Doctrine2?

How can I specify the columns that are used for the foreign key relation with Class Table Inheritance in Doctrine 2? For example, take the following two classes: /** * @Entity * @InhertanceType("JOINED") * @DiscriminatorColumn(name="type",…
Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
3
votes
1 answer

Django multi-table inheritance different from Postgres table inheritance

So I'm looking at Django's multitable inheritance, and how it differs from Postgres' table inheritance. Say I have the following models: models.py class Mayor(models.Model): name = models.CharField(max_length=255) class City(models.Model) …
3
votes
2 answers

How to convert entity leaving it id

There are some entities (Region, Country, City) which used STI (or even CTI). How it possible convert Country to City leaving old id?
Koc
  • 2,375
  • 2
  • 22
  • 26