> OK this is what happens here, the relationship you want to establish is:
>
> product_table ----(product_table.id=collections.parent_id)--->
> collections ----(collections.child_id=product_table.id)---->
> product_table
>
> so above, youve got product_table.id, youve got the collections table
> (secondary), and youve got the primary join although i think you mean
> for it to be
> "primaryjoin
> =collections_products_table.c.parent_id==product_table.c.id". the
> mapper also needs you to give it the secondaryjoin,
> "secondaryjoin
> =collections_products_table.c.collection_id==product_table.c.id",
> because it sees that there is more than one way to make a join between
> "products" and "collections" so it cant go any further (you could say
> that it should use the process of elimination since you gave it the
> primaryjoin, but thats a topic for another discussion :) ). so setup
> primaryjoin and secondaryjoin and youre good to go.
Thanks Mike! I think I speak for many here in saying that we really
appreciate how helpful you are on this list in addition to all the work
that has obviously gone into SA itself! ( Looking forward to that book,
btw! )
I had to rejiggle some column names to match your example but for any
interested, below is what works ( so far! ). I'm sure it would have
taken me hours of frustration to figure it out myself. If something
looks funny in the below and anyone wants to correct me, please do!
product_table = Table("products", metadata,
Column( "id", Integer, primary_key=True ),
Column( "name", String(100) ),
Column( "description", String, nullable=True ),
Column( "ordering", Integer, default=1 ),
Column( "price", Numeric, default=0.00 ),
Column( "pst", Numeric, default=0.06 ),
Column( "gst", Numeric, default=0.07 ),
Column( "plt", Numeric, default=0 ),
# is this product a collection ( basket, upgrade )
Column( "collection", Boolean, default='0', nullable=False ),
)
# many to many of collection-products to products
collections_products_table = Table('collections_products', metadata,
Column('parent_id', Integer, ForeignKey('products.id') ),
Column('child_id', Integer, ForeignKey('products.id') ),
)
class Product(object):
pass
assign_mapper(session.context, Product, product_table, properties={
'children': relation( Product, secondary=collections_products_table,
primaryjoin =
collections_products_table.c.parent_id==product_table.c.id,
secondaryjoin =
collections_products_table.c.child_id==product_table.c.id,
backref='products', lazy=True ),
})
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---