I tried to get the association object to work, but I think I don't get it.
The situation is as follows:
I have products. Those products have suppliers (many-to-many). The different suppliers have different prices for a product.
So what I would like to do is this:
# Assume everything is working
p = query(Product).get(1)
print p.suppliers
# This would print [<id=2 name='Apple Computer'>, <id=3 name='Unit 4'>]
print p.suppliers.price
# This would print Decimal('178.00')
This is the code I am using. While it does not give any errors, it won't work either ;-)
products = Table('products', metadata,
Column('id', Integer, Sequence('products_id_seq',optional=False), primary_key=True),
Column('name', Unicode(255), nullable=False),
Column('price', Numeric, nullable=False),
)
suppliers = Table('suppliers', metadata,
Column('id', Integer, Sequence('suppliers_id_seq',optional=False), primary_key=True),
Column('name', Unicode(255), nullable=False)
)
product_suppliers = Table('product_suppliers', metadata,
Column('product_id', Integer, ForeignKey("products.id")),
Column('supplier_id', Integer, ForeignKey("suppliers.id")),
Column('price', Numeric, nullable=False),
Column('precedence', Integer, nullable=False)
)
class Product(object):
def __init__(self, code, name):
self.name = unicode(name)
self.price = Decimal(price)
class ProductAssociation(object):
pass
class Supplier(object):
def __init__(self, name):
self.name = unicode(name)
mapper(Supplier, suppliers)
mapper(ProductAssociation, product_suppliers,
primary_key = [product_suppliers.c.product_id, product_suppliers.c.supplier_id]
)
mapper(Product, products, properties={
'orderproducts': relation(OrderProduct, lazy=False, cascade="all, delete-orphan", backref='product'),
'suppliers': relation(ProductAssociation, lazy=False, cascade="all, delete-orphan", association=Supplier)
})