PostgreSQL table inheritance and ORM

Hello,

If I use models inheritance, in Django, with PostgreSQL as a database, does the generated SQL code create SQL tables inheritance ?

Something like this, for instance:

CREATE TABLE users (
    uid CHAR(16) NOT NULL
    ...
);

CREATE TABLE manager (
    ...
) INHERITS (users);

CREATE TABLE team_owner (
    ...
) INHERITS (users);

Thank you.

No. If you use abstract base models, then they don’t have their own tables and are simply used to avoid repeating field definitions in code. If you use concrete base models, they get their own table, and the child tables get a OneToOneField to the parent table. Creating a child instance also creates the related parent instance.

There’s no inheritance from the database point of view.

See the docs: Models | Django documentation | Django

Two extra resources you might be interested into

In the light of the current Postgres recommendations I’ll close the ticket as wont-fix as it has been stagnant for 10 years now.

1 Like