This works. However although type=='file' is the only time I want/need
to have a subclass for, I still need to know the original value of type.
Is there a way in SA to basically get two copies of the column? Maybe I
could return a tuple from MyRow.__getitem__ ?
The best think I can think of is to create a view (PostgreSQL):
CREATE OR REPLACE VIEW nodehierarchy_view AS SELECT *, type AS poly
FROM nodehierarchy;
and map my classes against the view, that way I have two copies of the
type columns.
To complicate things slightly more I am joining against a type table,
who's primary key is a text field. So typically when I just want to know
the name of the type I do node.type, but sometimes I need additional
information and do a node.Type.other_info. Right now the relation is
commented out since it isn't working.
node_mapper = mapper(Node, nodehierarchy_table, extension=MyExt(),
polymorphic_on=nodehierarchy_table.c.type, polymorphic_identity='node',
properties = {
#'Type' : relation (Type,
primaryjoin=(type_table.c.name==nodehierarchy_table.c.type), uselist=False),
Michael Bayer wrote:
> the feature that would best support this would be to allow the
> polymorphic_on to be a callable so that any user-defined functionality
> can be run on the given row in order to determine the discriminator
> value.
>
> Here's a slightly less convenient way to get that effect now:
>
> class MyRow(UserDict):
> def __init__(self, row, discriminator):
> self.data = row
> self.discriminator = discriminator
>
> def __getitem__(self, key):
> if key is self.discriminator and self.data[key] != 'file':
> return 'default_value'
> else:
> return self.data[key]
>
> class MyExt(MapperExtension):
> def translate_row(self, mapper, context, row):
> return MyRow(row, mapper.polymorpic_on)
>
> mapper(MyClass, mytable, extension=MyExt())
>
> allowing a callable polymorphic_on wouldn't be a hard feature to add
> and its been planned to be eventually added.
>
> On Oct 30, 2008, at 1:19 PM, David Gardner wrote:
>
>
>> I have a situation similar to
>> http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_mapper_inheritance_single
>> with the exception that I have about 50 different types, but only
>> one of
>> them I want to subclass. So I was wondering id there was some option
>> to
>> polymorphic_identity to give it a list of values.
>>
>> Currently what I do is I map the subclass against a query:
>> sql_file_node = select ([nodehierarchy_table],
>> asset_table.c.type=='file').alias ('file_node_query')
>> mapper(Node, nodehierarchy_table .....)
>> mapper(FileNode, sql_file_node......)
>>
>> what I would like to do:
>> node_mapper = mapper(Node, nodehierarchy_table,
>> polymorphic_on=nodehierarchy_table.c.type, ....)
>> mapper(FileNode, inherits=node_mapper,
>> polymorphic_on=asset_table.c.type, polymorphic_identity='file' ....)
>>
>> However when I do this, SA complains that there "AssertionError: No
>> such
>> polymorphic_identity '<any other type>' is defined"
>>
>>
>> To complicate matters slightely this is a self referential table,
>> http://www.sqlalchemy.org/trac/browser/sqlalchemy/trunk/examples/adjacencytree/basic_tree.py
>>
>> This isn't a huge problem because I could continue to use SA the way I
>> currently am.
>>
>> --
>> David Gardner
>> Pipeline Tools Programmer, "Sid the Science Kid"
>> Jim Henson Creature Shop
>> [EMAIL PROTECTED]
>>
>>
>>
>>
>
>
> >
>
>
--
David Gardner
Pipeline Tools Programmer, "Sid the Science Kid"
Jim Henson Creature Shop
(323) 802-1717 [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---