Yeah, I tried that! But still I get a:

AttributeError: ("type object 'Supplier' has no attribute 'supplier'",)

Same with the example in the docs (but then for article)!

I have included a ready-to-run testscript.


from sqlalchemy import *

engine = create_engine('sqlite:///::memory::', echo=True)

metadata = MetaData()

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, name, price):
		self.name = unicode(name)
		self.price = 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],
    properties = {
        'supplier':relation(Supplier)
    }
)

mapper(Product, products, properties={
	'suppliers': relation(ProductAssociation, lazy=False, cascade="all, delete-orphan", association=Supplier)
})

# Recreate all tables for testing purposes
engine.echo = False
metadata.drop_all(engine=engine)
metadata.create_all(engine=engine)
engine.echo = True

session = create_session(bind_to=engine)

product = Product(name='MacBook', price='100')
supplier1 = Supplier('Apple')
supplier2 = Supplier('Unit 4')

product.suppliers.append(supplier1)
product.suppliers.append(supplier2)

session.save(product)
session.flush()

On May 28, 2006, at 6:37 PM, Michael Bayer wrote:

yah no prob, you just need this:

mapper(ProductAssociation, product_suppliers,
    primary_key = [product_suppliers.c.product_id, product_suppliers.c.supplier_id],
    properties = {
        'supplier':relation(Supplier)
    }
)



On May 28, 2006, at 7:05 AM, Koen Bok wrote:

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)
})


Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to