On Thu, 2008-05-15 at 11:30 -0400, J. Cliff Dyer wrote:
> On Thu, 2008-05-15 at 11:24 -0400, J. Cliff Dyer wrote:
> > I'm trying to implement polymorphic inheritance using the
> > sqlalchemy.ext.declarative, but the field that I want to use for the
> > polymorphic_on is not in my polymorphic base table, but at the other end
> > of a many-to-one relationship. We have items of many types, and in the
> > item table, we have a type_id field which contains an integer, but in
> > the item_type table, that integer is mapped to a descriptive name.
> > Essentially, I'd like to use that name as my polymorphic_identity. I've
> > tried to implement this with declarative classes below, but I get an
> > error, also shown below.
> >
> >
OK. The old example was wrong in other ways. That was a problem with
__table__ vs. __tablename__, but I got past that, and got my
polymorphism working on the basic case, where I just use the type_id
directly as follows:
class ItemType(Declarative):
__tablename__ = 'item_type'
type_id = sa.Column('type_id', sa.Integer, primary_key=True)
description = sa.Column('description', sa.Unicode(250))
class Item(Declarative):
__tablename__ = 'item'
item_id = sa.Column('item_id', sa.Integer, primary_key=True)
type_id = sa.Column('type_id', sa.Integer,
sa.ForeignKey('item_type.type_id'))
short_title = sa.Column('short_title', sa.Unicode(100))
item_type = orm.relation('ItemType', backref='items')
__mapper_args__ = {'polymorphic_on': type_id }
But I'd still like to do something like this:
__mapper_args__ = {'polymorphic_on': item_type.description }
but I get the following error:
Traceback (most recent call last):
File "polymorph_test.py", line 28, in ?
class Item(Declarative):
File "polymorph_test.py", line 34, in Item
__mapper_args__ = {'polymorphic_on': item_type.description }
AttributeError: 'PropertyLoader' object has no attribute 'description'
or from the following:
__mapper_args__ = {'polymorphic_on': ItemType.description }
Traceback (most recent call last):
File "polymorph_test.py", line 28, in ?
class Item(Declarative):
File "/net/docsouth/dev/lib/python/sqlalchemy/ext/declarative.py",
line 257, in __init__
cls.__mapper__ = mapper_cls(cls, table, properties=our_stuff,
**mapper_args)
File "/net/docsouth/dev/lib/python/sqlalchemy/orm/__init__.py", line
566, in mapper
return Mapper(class_, local_table, *args, **params)
File "/net/docsouth/dev/lib/python/sqlalchemy/orm/mapper.py", line
181, in __init__
self.__compile_properties()
File "/net/docsouth/dev/lib/python/sqlalchemy/orm/mapper.py", line
684, in __compile_properties
col = self.mapped_table.corresponding_column(self.polymorphic_on) or
self.polymorphic_on
File "/net/docsouth/dev/lib/python/sqlalchemy/sql/expression.py", line
1735, in corresponding_column
target_set = column.proxy_set
File "/net/docsouth/dev/lib/python/sqlalchemy/orm/mapper.py", line
638, in __getattribute__
return getattr(getattr(cls, clskey), key)
AttributeError: 'InstrumentedAttribute' object has no attribute
'proxy_set'
How can I use this field for polymorphism? Is it possible?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---