On Dec 7, 2011, at 2:53 AM, kris wrote:

> 
> I am using a single table scheme to store for a set of resource types.
> 
> I would like to load a specific class if a mapper is defined and use the base 
> class if no
> more specific version can be found.   Is there a way to do this?
> i.e.
> resource  = Table('resource', 
>                            Column('resource_type',  String),
>                            ...
> 
> I also have a set of predefined helper classes
> class Resource, Resource_A, Resource_B, 
> 
> mapper(Resource, ...
>        polymorphic_on = resource.c.resource_type,                             
>                                       
>        #polymorphic_identity = ''*"  # I think I need something like this
> mapper(Resource_A,...
>        polymorphic_on = resource.c.resource_type,                             
>                                       
>        polymorphic_identity = ''A"
> mapper(Resource_B,...
>        polymorphic_on = resource.c.resource_type,                             
>                                       
>        polymorphic_identity = ''B"
> 
> 
> Now if I have inserted types 
> A = Resource_A()
> B = Resource_B()
> C = Resource()
> C.resource_type = "C"
> D = Resource()
> D.resource_type = "D"
> session.add([A,B,C,D])
> 
> And then I do 
> session.query(Resource)
> 
> Is there a way to specify the polymorphic_identity of Resource is default ?

sure, just put a polymorphic_identity on your base Resource class and that's 
the class you'll get if that identity value is the one retrieved from the row.

However in your example above, the Resource you have where you stuck a "D" on 
it, you'd get a Resource_D back when querying.  It's best not to manually set 
discriminators like that as you'll have inconsistent typing between the "to be 
flushed" set of objects and the "returned from a query" set.

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to