Accessing relations of a table with inheritance

Active3 hr before
Viewed126 times

7 Answers


I'm trying to implement a database that has inheritance between some tables, there is three tables involved in the question problem: Customers, Users and Addresses (actually there is more tables involved, but with the same problem, so..).,I assume that you're using PostgreSQL table inheritance which, unfortunately, doesn't work quite as we would expect. In particular, although records from child tables appear in selects from parent table, they are not physically recorded there, and thus their ids can't be used in foreign keys referencing parent tables.,So My problem is that I want that table 'Customers' to has the same relationship that 'Users' has with 'Addresses', cause Customers is inherits from it. I also try to insert data to 'Addresses' with an ID from 'Customers', but this give an foreign key constraint violation, the value doesn't exists in table "myDb.users" error,The Customers table inherits from Users table, and the Users table has a relationship with the Addresses table (1 to many, respectively).

You may consider implementing inheritance using classic approach:

CREATE TABLE Users(id INT PRIMARY KEY, user_property INT);
CREATE TABLE Customers(id INT PRIMARY KEY REFERENCES Users, customer_property INT);
CREATE TABLE Addresses(user_id INT REFERENCES Users, address TEXT);
load more v

When designing a database, we sometimes come across situations where there are multiple types of entities that we are modeling, but we'd like them to all have certain attributes or relations in common. Using "sub-type" tables is a simple way to implement table inheritance in SQL Server.,If we want to output a "Type" column (Student, Teacher, Parent) when querying the People table, we must outer join to all the 3 "sub-tables" and determine if any matching rows exist., Notice that we cannot add teacher information for Bob, since he is a student: , So, everything works beautifully. We have our base table, we have our sub-tables, and the inheritance allows us to work with the different entities all the same way, but to also treat each of them specifically on their own as necessary.

That gives us a schema like this:

create table People(PersonID int primary key, Name varchar...etc...)

create table Students(PersonID int primary key references People(PersonID), ...)

create table Teachers(PersonID int primary key references People(PersonID), ...)

create table Parents(PersonID int primary key references People(PersonID), ...)
load more v

The diagram below shows the table client and two views: individual and company:,The diagram below presents two tables, individual and company, and a view (the blue one) called client.,In class Client we distinguish two subtypes: Individual and Company. This specialization is disjoint (client can be an individual or a company) and complete (these are all possible subtypes for supertype).,Access to supertype rows is optimal (it is simple to have a list of all clients, it’s not necessary to make costly joins)

CREATE VIEW individual AS
select id, address, name, surname
from client
where status = 'i';
load more v

PostgreSQL implements table inheritance, which can be a useful tool for database designers. (SQL:1999 and later define a type inheritance feature, which differs in many respects from the features described here.),Inheritance does not automatically propagate data from INSERT or COPY commands to other tables in the inheritance hierarchy. In our example, the following INSERT statement will fail:,These deficiencies will probably be fixed in some future release, but in the meantime considerable care is needed in deciding whether inheritance is useful for your application.,Given the sample data from the PostgreSQL tutorial (see Section 2.1), this returns:

Let's start with an example: suppose we are trying to build a data model for cities. Each state has many cities, but only one capital. We want to be able to quickly retrieve the capital city for any particular state. This can be done by creating two tables, one for state capitals and one for cities that are not capitals. However, what happens when we want to ask for data about a city, regardless of whether it is a capital or not? The inheritance feature can help to resolve this problem. We define the capitals table so that it inherits from cities:

   name text,
   population float,
   altitude int-- in feet

CREATE TABLE capitals(
   state char(2)
) INHERITS(cities);
load more v

Creating Tables that Contains Supertype and Subtype Objects, Creating Tables that Contains Supertype and Subtype Objects ,Creating a Parent or Supertype Object, Creating a Parent or Supertype Object

Example 2-12 Creating an Object Type as NOT FINAL with a FINAL Member Function

DROP TYPE person_typ FORCE;
--above necessary
if you have previously created object

   idno NUMBER,
   name VARCHAR2(30),
   phone VARCHAR2(20),
load more v

Current relational databases unfortunately don't support the inheritance directly, therefore you need to transform it into "plain" tables. There are generally 3 strategies for doing so:, Reconnecting with a previous professor then asking right away for a reference letter , 1 You claim current relational databases don't support inheritance. What about postgresql? – Climax Dec 26 '17 at 14:45 ,Making statements based on opinion; back them up with references or personal experience.

For more on what this actually means and some pros and cons, please see the links provided in my original post, but in a nutshell the (3) should probably be your default unless you have a specific reason for one of the other two. You can represent the (3) in the database simply like this:

   person_id int PRIMARY KEY
   --Other fields...

   civil_id int PRIMARY KEY REFERENCES person(person_id)
   --Other fields...

   worker_id int PRIMARY KEY REFERENCES person(person_id)
   --Other fields...

   event_id int PRIMARY KEY,
   person_id int REFERENCES person(person_id)
   --Other fields...
load more v

Relationships with Joined Inheritance,Relationships with Single Table Inheritance,Relationships with Concrete Inheritance,Joined Table Inheritance Relationships with Joined Inheritance Loading Joined Inheritance Mappings

class Employee(Base):
   __tablename__ = 'employee'
id = Column(Integer, primary_key = True)
name = Column(String(50))
type = Column(String(50))

__mapper_args__ = {
   'polymorphic_identity': 'employee',
   'polymorphic_on': type
load more v

Other "accessing-table" queries related to "Accessing relations of a table with inheritance"