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.
> 
> 
> class ItemType(Declarative):
>     __table__ = sa.Table('item_type', Declarative.metadata)
>     type_id = sa.Column('type_id', sa.Integer, primary_key=True)
>     description = sa.Column('description', sa.Unicode(250))
>     item = orm.relation('Item', backref='item_type')
> 
> class Item(Declarative):
>     __table__ = sa.Table('item', Declarative.metadata)
>     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))
>     __mapper_args__ = {'polymorphic_on': ItemType.description }
> 
> 
> ### Traceback follows:
> 
> """
> Traceback (most recent call last):
>   File "polymorph_test.py", line 23, in ?
>     class ItemType(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
> 653, in __compile_properties
>     self._compile_property(key, prop, False)
>   File "/net/docsouth/dev/lib/python/sqlalchemy/orm/mapper.py", line
> 719, in _compile_property
>     raise exceptions.ArgumentError("Column '%s' is not represented in
> mapper's table.  Use the `column_property()` function to force this
> column to be mapped as a read-only attribute." % str(c))
> sqlalchemy.exceptions.ArgumentError: Column 'description' is not
> represented in mapper's table.  Use the `column_property()` function to
> force this column to be mapped as a read-only attribute.
> """
> 
> Apparently it can't find the description column, even though it's
> clearly defined on ItemType.

For what it's worth, mysqldump yields the following for item_type:

--
-- Table structure for table `item_type`
--

DROP TABLE IF EXISTS `item_type`;
CREATE TABLE `item_type` (
  `type_id` int(11) NOT NULL auto_increment,
  `description` char(250) collate utf8_unicode_ci default NULL,
  `ts` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  PRIMARY KEY  (`type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to