Problems Using Inheritance with Multiple Subclasses

5 views
Skip to first unread message

Jeff Balogh

unread,
May 17, 2007, 4:09:13 PM5/17/07
to SQLElixir
I'm trying to create multiple classes that have almost everything in
common, so I tried making a base class that the other classes inherit
from. However, only one table is made, with all data fields stuffed
into the base class's table. Here's an approximation of what I'm
trying to do:

from elixir import *

class Foo(Entity):
#some common functionality here
has_field('foo_field', DateTime)

class Bar(Foo):
has_field('bar_field', Unicode)

class Baz(Foo):
has_field('baz_field', Numeric)

metadata.connect("sqlite:///:memory:")
metadata.engine.echo = True
metadata.create_all()

And this is the SQL that gets outupt:

CREATE TABLE inheritance_foo (
foo_field TIMESTAMP,
id INTEGER NOT NULL,
bar_field TEXT,
baz_field NUMERIC(10, 2),
PRIMARY KEY (id)
)

Am I doing something wrong, or has someone figured this problem out
already?

Thanks,
jeff

Daniel Haus

unread,
May 18, 2007, 3:59:52 AM5/18/07
to SQLElixir
Hi Jeff,

the phenomenon you just discovered is called "single-table
inheritance"
and is meant to work this way. We're all aching for true polymorphic
multitable inheritance (with one table for each subclass), but no one
implemented it yet. The first try to do this - a couple of months ago
-
ended in a mess.

If you really need multitable inheritance, you might try stepping one
level down and do it manually through SQLAlchemy for now.

Hope that helped,
Daniel

Jeff Balogh

unread,
May 18, 2007, 1:56:19 PM5/18/07
to SQLElixir
Oh well; I figured it would be a bit harder with the database mapping
involved. Thanks for your help.
Reply all
Reply to author
Forward
0 new messages