Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you have here is not clearly either type because you duplicate some columns but not others.</p> <p>From the <a href="http://docs.sqlalchemy.org/en/latest/orm/inheritance.html" rel="nofollow">docs</a>:</p> <blockquote> <p>SQLAlchemy supports three forms of inheritance: <strong>single table inheritance</strong>, where several types of classes are represented by a single table, <strong>concrete table inheritance</strong>, where each type of class is represented by independent tables, and <strong>joined table inheritance</strong>, where the class hierarchy is broken up among dependent tables, each class represented by its own table that only includes those attributes local to that class.</p> </blockquote> <p>Because you duplicate the <code>created</code> and <code>modified</code> columns, this is a candidate for concrete inheritance. However, because you expect <code>Employee</code> to be an incomplete entity without columns from <code>Person</code>, this is also joined table inheritance.</p> <p>I suggest you try to make this fit into joined table inheritance, but I'm not entirely sure how the mapper will handle the presence of duplicated columns. It is possible in joined table inheritance to manipulate the subclasses separately as relations, but I'm not sure how they will be merged, if they can be merged, or what mapper configuration is available to handle which one is used. If there is a way, <a href="http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#advanced-control-of-which-tables-are-queried" rel="nofollow">this section</a> will probably show you how.</p> <p>However here is something to get you started. <a href="http://docs.sqlalchemy.org/en/latest/orm/mapper_config.html#sqlalchemy.orm.mapper" rel="nofollow"><code>polymorphic_on</code> can also be any sql expression (scroll down to the polymorphic_on argument)</a>--it doesn't have to be a column. If your only subclass is the <code>engineers</code> table, you use an <code>EXISTS</code> subquery in your discriminator.</p> <p>Below is some untested code that will probably not work without messing with it a bit--it just shows you the pattern you'll have to use.</p> <pre><code>people_table = Table('people', metadata, Column('id', Integer, primary_key=True), Column('email', String, unique=True), Column('inactive', Boolean, default=False), ) engineers_table = Table('engineers', metadata, Column('id', Integer, ForeignKey('people.id'), primary_key=True), Column('engineer_info', String), ) class Person(object): pass class Engineer(Person): pass discriminator = case( [ (exists().where(people_table.c.id==engineers_table.c.id), 'engineer'), ], else_='person') mapper(Person, people_table, polymorphic_identity='person', polymorphic_on=discriminator) mapper(Engineer, engineer_table, polymorphic_identity='engineer') </code></pre> <p>Needless to say, if it's at all possible you should, for your own sanity:</p> <ol> <li>Add a discriminator column to your <code>person</code> table.</li> <li>Get rid of the <code>created</code> and <code>modified</code> columns in the <code>engineer</code> table.</li> </ol>
    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