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]
>
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---