this assumes each House will point to a unique Roof object (i.e., its a one-to-one relationship to a Roof). also if you use the latest trunk (or 2.3 when i release it this weekend) you can declare the mappers in any order.
houses
----------
house_id integer pk
name varchar
roofs
-------
roof_id integer pk
house_id integer references houses(house_id) -- or put roof_id on the houses table, same diff
name varchar
type varchar
concrete_roofs
--------------------
roof_id integer pk references roofs(roof_id)
hardness varchar
wood_roofs
----------------
roof_id integer pk references roofs(roof_id)
treename varchar
metadata = BoundMetaData('some://db')
(houses, roofs, concete_roofs, wood_roofs) = [Table(name, metadata, autoload=True) for name in ['houses', 'roofs', 'concrete_roofs', 'wood_roofs']]
class House(object):pass
class Roof(object):pass
class ConcreteRoof(Roof):pass
class WoodRoof(Roof):pass
mapper(House, houses, properties={
'roof':relation(Roof, uselist=False, cascade="all, delete-orphan", backref=backref("house"))
})
roof_union = polymorphic_union(
{
'concrete':concrete_roofs.join(roofs),
'wood':wood_roofs.join(roofs),
},
None, 'roof_union'
)
mapper(Roof, roofs, select_table=roof_union, polymorphic_on=roofs.c.type)
mapper(ConcreteRoof, concrete_roofs, inherits=Roof, polymorphic_identity='concrete')
mapper(WoodRoof, wood_roofs, inherits=Roof, polymorphic_identity='wood')
...and if that all works (which it should.....) then we really have something cool going on here...
On Jun 16, 2006, at 7:43 AM, Koen Bok wrote:
Maybe a little metaphor to make it more simple.
Let's say I make houses. The roof of the house can either be concrete or wood. Concrete and wood come in many types.
Design:
House
id
name
Wood
id
name
treename
Concrete
id
name
hardness
Now if I have a House object it should have a roof relation which either links to a Wood object or a Concrete object. It NEEDS to be linked to one of them.
Like this:
>>> house = session.query(House).get(1)
>>> print house.roof
<Concrete id=123 name=SuperBeautiful hardness=500>
>>> house = session.query(House).get(2)
>>> print house.roof
< Wood id=321 name=Oak treename='Oak Tree'>
On 16-jun-2006, at 5:24, Michael Bayer wrote:
whats wrong with this ?
order_types
---------------
type_id
name
from_stock_id
to_stock_id
?
On Jun 15, 2006, at 6:44 PM, Koen Bok wrote:
Within my organisation we have orders. Orders move products between three entities: Suppliers, Stocks and Customers.
The type of an order gets defined by the origin and the destination it is moving products. This way we can have the following types of orders:
From: To: -> Stock BUYORDER
Stock -> Supplier RETOURBUYORDER
Stock -> Stock INTERNORDER
Stock -> Customer SELLORDER
Customer ! -> Stock RETOURSELLORDER
How can I design objects/mappers to do this:
>>> order = session.query(Order).get(1)
>>> print order.type
<Type id=123 name='INTERNORDER' from=<Stock id=2> to=<Stock id=4>>
>>> order = session.query(Order).get(2)
>>> print order.type
<Type id=456 name='SELLORDER' from=<Stock id=4> to=<Customer id=1928>>
_______________________________________________
Sqlalchemy-users mailing list
_______________________________________________
Sqlalchemy-users mailing list