4

I found an example of how to do single table inheritance using Class mappings.

http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#single-table-inheritance

But for the life of me, I cannot find an example of how to do this with classic mapper so that I can keep my classes and persistent mappings separate.

How do I convert this example into classic mapping? I am clear on creating the tables, just not sure how to actually structure the mapper.

In the example, there are the following types defined:

class Employee(Base):

class Manager(Employee):

class Engineer(Employee):

Assuming I have created the appropriate table:

employee = Table(...Column(type...))

How do I write code for the mapper so that both Manager and Engineer live in the same table (single table inheritance) discriminated by type ("manager", "engineer" or otherwise employee)?

Thanks.

Alex Paransky
  • 985
  • 2
  • 10
  • 21

1 Answers1

9

Manually mapping class inheritance hierarchies is laborious and not something I'd recommend, but here goes. Start by defining your table. Since using single table inheritance, it must include all the required columns:

metadata = MetaData()

employee = Table(
    'employee',
    metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(50)),
    Column('type', String(20)),
    Column('manager_data', String(50)),
    Column('engineer_info', String(50))
)

The plain Python classes:

class Employee:

    def __init__(self, name):
        self.name = name

class Manager(Employee):

    def __init__(self, name, manager_data):
        super().__init__(name)
        self.manager_data = manager_data

class Engineer(Employee):

    def __init__(self, name, engineer_info):
        super().__init__(name)
        self.engineer_info = engineer_info

And the classical mappings:

mapper(Employee, employee,
       polymorphic_on=employee.c.type,
       polymorphic_identity='employee',
       exclude_properties={'engineer_info', 'manager_data'})


mapper(Manager,
       inherits=Employee,
       polymorphic_identity='manager',
       exclude_properties={'engineer_info'})


mapper(Engineer,
       inherits=Employee,
       polymorphic_identity='engineer',
       exclude_properties={'manager_data'})

Note how you have to limit the mapped properties manually in each mapper, which will become hard to maintain with larger hierarchies. When using Declarative all that is handled for you.

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
  • 1
    Your note is well taken. After applying this example to my own classes, I see how laborious this process becomes. – Alex Paransky Jun 22 '17 at 02:19
  • Declarative seems to handle creating the table and mappers a bit differently for single table inheritance and seems to be able to incrementally define the table as you add subclasses. In the example from docs the only time `exclude_properties` is used is in `Engineer`, excluding *manager_data*. I tried looking around how you'd better emulate that manually, but could not find a way as of yet. So perhaps this answer might improve in the future. And even with that you'd still end up having to track what to exclude and what to add to table in larger hierarchies. – Ilja Everilä Jun 22 '17 at 05:52