make ElProduct and FoodProduct inherit GeneralProduct. use joined_inheritance, and left-outer-joins instead of unions.
at the end, query(GeneralProduct) will give u all the types.
if u want just the GeneralProduct alone, that would be something like
query(GeneralProduct).filter( GeneralProduct.type=='GeneralProduct')
anyway, attached are a dbcook setup and the output of
$ python ex.py generate
which can be used more or less as source to start...
On Tuesday 17 June 2008 19:49:37 Moshe C. wrote:
> I have 3 tables:
>
> general_product {id, name, price} mapped to GeneralProduct
> electrical_product {id, name, price, voltage} mapped to
> ElectricalProduct
> food_product {id,name, price, calories} mapped to FoodProduct
>
> 1) I want to create a union query on the above 3 selecting only id,
> name and price (which are common fields).
> How do I do that? Could find the doc for union only in the non-orm
> case.
>
> 2) Since this smells of inheritance, I read about class inheritance
> mapping but couldn't decide if and which method applies here.
> Help would be greatly appreciated.
>
> TIA
> Moshe
>
>
>
>
>
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
ex.py
Description: application/python
========= generated SA set-up
table_ElProduct = Table( 'ElProduct', meta,
Column( 'voltage', Integer, ),
Column( 'db_id', Integer, ForeignKey( 'GenProduct.db_id', ), autoincrement= False, primary_key= True, ),
)
table_FoodProduct = Table( 'FoodProduct', meta,
Column( 'calories', Integer, ),
Column( 'db_id', Integer, ForeignKey( 'GenProduct.db_id', ), autoincrement= False, primary_key= True, ),
)
table_GenProduct = Table( 'GenProduct', meta,
Column( 'price', Integer, ),
Column( 'name', String(length=100, convert_unicode=False, assert_unicode=None), ),
Column( 'atype', type_= String(length=100, convert_unicode=False, assert_unicode=None), ),
Column( 'db_id', primary_key= True, type_= Integer, ),
)
meta.create_all()
class ElProduct( GenProduct):
props = ['db_id', 'price', 'name', 'voltage']
class FoodProduct( GenProduct):
props = ['db_id', 'price', 'calories', 'name']
class GenProduct( Base):
props = ['db_id', 'price', 'name']
mapper_ElProduct = mapper( ElProduct, table_ElProduct,
inherit_condition= table_ElProduct.c.db_id == table_GenProduct.c.db_id,
inherits= mapper_GenProduct,
polymorphic_identity= 'ElProduct',
)
mapper_FoodProduct = mapper( FoodProduct, table_FoodProduct,
inherit_condition= table_FoodProduct.c.db_id == table_GenProduct.c.db_id,
inherits= mapper_GenProduct,
polymorphic_identity= 'FoodProduct',
)
mapper_GenProduct = mapper( GenProduct, table_GenProduct,
polymorphic_identity= 'GenProduct',
polymorphic_on= table_GenProduct.c.atype,
select_table= outerjoin( outerjoin( table_GenProduct, table_FoodProduct, table_FoodProduct.c.db_id == table_GenProduct.c.db_id, ), table_ElProduct, table_ElProduct.c.db_id == table_GenProduct.c.db_id, ),
)
mapper_GenProduct1 = mapper( GenProduct, table_GenProduct.select( table_GenProduct.c.atype == 'GenProduct', ).alias( 'bz4GenProduct' ),
non_primary= True,
)
psub_GenProduct = ( table_GenProduct.c.atype != 'GenProduct' )
========= eo generated SA set-up
