an example of concrete inheritance with polymorphic_union is needed

53 views
Skip to first unread message

Richard Rosenberg

unread,
Aug 15, 2012, 4:00:55 AM8/15/12
to sqlal...@googlegroups.com
I think at this point, the docs are simply making it worse for me. Is there an example out there that is declarative and concise?

This is a really simple scenario involving a single "header" table, and multiple (identical) "detail" tables, as in:

headertable
  1.   id int
  2.   namekey varchar

detail1
  1.   id integer
  2.   headerid integer, fk headertable.id
  3.   groupid integer
  4.   somevalue varchar

And so on, ad nauseum, detail2. . .detailN

Employees, engineers, and managers. . .Is.Not.Working for me. Is there something better out there. . ? I can make it AbstractConcreteBase or ConcreteBase, or whatever at this point, any direction in the way of best practice or gotchas is appreciated too.  

Thanks. . .

Michael Bayer

unread,
Aug 15, 2012, 11:09:52 AM8/15/12
to sqlal...@googlegroups.com
First note that the main inheritance docs for "latest" have been updated to be fully declarative for "joined" and "single" inheritance: http://docs.sqlalchemy.org/en/latest/orm/inheritance.html .   Concrete will follow.

The next step is identifying one of three patterns: single, joined, or concrete table inheritance.    If our docs aren't enough, here are Fowler's description of each : http://martinfowler.com/eaaCatalog/singleTableInheritance.html http://martinfowler.com/eaaCatalog/classTableInheritance.html (this is what we call "joined"), http://martinfowler.com/eaaCatalog/concreteTableInheritance.html.   Here's another extremely in-depth description of the various inheritance patterns, including ones we don't support: http://www.agiledata.org/essays/mappingObjects.html#MappingInheritance .    I should actually consider adding Fowler's links to our own docs, that's a good idea.

So at this point, you'd have decided which of those three patterns you're using.   I can already tell you that its "joined", because you have two separate tables (so not single) and your "base" table has a column that is not in the "sub" table (so not concrete).

From there, you can pretty much swap out the names in our Employee example at http://docs.sqlalchemy.org/en/latest/orm/inheritance.html#joined-table-inheritance to get at the initial "headertable" and "detail1".  

I've attached that, laying out Header and Detail1 explicitly.  Then, since you said you have "detail2, 3, 4,...N" I illustrate a function to create more Detail classes dynamically.  You can run this example as is, as it creates its own SQLite database.   I hope it helps!

details.py
Reply all
Reply to author
Forward
0 new messages