Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the proper way to model this type of inheritance with SQLAlchemy?
    primarykey
    data
    text
    <p>I have a pair of PostgreSQL tables that look somewhat like this (simplified example):</p> <pre><code>CREATE TABLE people ( id SERIAL PRIMARY KEY, created timestamp with time zone DEFAULT now() NOT NULL, modified timestamp with time zone NULL, email varchar NOT NULL UNIQUE, inactive boolean NOT NULL DEFAULT False ); CREATE TABLE engineers ( id integer NOT NULL REFERENCES people(id) ON DELETE CASCADE ON UPDATE CASCADE UNIQUE, created timestamp with time zone DEFAULT now() NOT NULL, modified timestamp with time zone NULL, login_name varchar NOT NULL UNIQUE, PRIMARY KEY(id) ); </code></pre> <p><code>engineers</code> inherits from <code>people</code> because the "id" column <code>people</code> passes through to <code>engineers</code> as a foreign key. This column is also defined as a primary key in both tables. If I wanted query the login_name using someone's email address, it could be done like this via SQL:</p> <pre><code>SELECT p.email FROM engineers e JOIN people p ON p.id = e.id WHERE p.inactive = False AND e.login_name = 'john.smith'; </code></pre> <p>How do I model this relationship using SQLAlchemy's declarative style and perform a similar query? It seems like "<a href="http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative.html#joined-table-inheritance" rel="nofollow">Joined Table Inheritance</a>" describes my usage scenario, but I don't have a discriminator column in my tables. I've also been trying to use "<a href="http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative.html#concrete-table-inheritance" rel="nofollow">Concrete Table Inheritance</a>" but am just managing to confuse myself. </p> <p>I'd appreciate any suggestions. Thanks!</p> <h1>Edited</h1> <p>The reason this table structure is the way it is is because a "person" might be an "engineer", "accountant", or "tester" (or some combination of the three). The <code>people</code> table contains the attributes that are common to everyone, such as a name, phone, number, date of hire, etc.</p> <p>The "created" and "modified" columns on the <code>engineers</code> table are not shared between <code>people</code>; the two columns are distinct to each table. These aren't absolutely necessary, they're just added by default to each table definition in the database and are used to track changes for audit/logging purposes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload