NoDamage
unread,Jul 28, 2009, 8:14:02 PM7/28/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to sqlalchemy
Is it possible to manually set the type column of a base class when
using single table inheritance? The reason I want to do this is
because I am importing data from an external source which does not
differentiate between the subclass types.
For example:
a_table = Table('a', metadata,
Column('id', Integer, primary_key=True),
Column('type', String(20)),
Column('name', String(20))
)
class A(object): pass
class B(A): pass
a_mapper = mapper(A, a_table, polymorphic_on=a_table.c.type,
polymorphic_identity='a', with_polymorphic='*')
b_mapper = mapper(B, inherits=a_mapper, polymorphic_identity='b')
Here, A is the base class and B is the subclass. I want to be able to
do this:
a = A()
a.type = 'b'
session.add(a)
result = session.query(A).all()
I expect the result to contain an instance of B because I have
explicitly set the type to 'b'. But right now, it looks like because I
have instantiated an instance of A, the type is overwritten as 'a'. Is
there any nice way to do this?