On Nov 9, 2007, at 11:05 PM, iain duncan wrote:
>
> # many to many of collection-products to products
> collections_products_table = Table('collections_products', metadata,
> Column('collection_id', Integer, ForeignKey('products.id') ),
> Column('product_id', Integer, ForeignKey('products.id') ),
> )
>
> #mapper
> assign_mapper(session.context, Product, product_table, properties={
> 'children': relation( Product, secondary=collections_products_table,
> lazy=True, )
> })
>
>
> or
> assign_mapper(session.context, Product, product_table, properties={
> 'children': relation( Product, secondary=collections_products_table,
> primaryjoin =
> collections_products_table.c.collection_id==product_table.c.id,
> lazy=True ),
> })
>
>
> Neither of the above work. Tg loads ok but when I try to make a
> product
> I get the following:
>
> ArgumentError: Error determining primary and/or secondary join for
> relationship 'children' between mappers 'Mapper|Product|products' and
> 'Mapper|Product|products'. If the underlying error cannot be
> corrected,
> you should specify the 'primaryjoin' (and 'secondaryjoin', if there is
> an association table present) keyword arguments to the relation()
> function (or for backrefs, by specifying the backref using the
> backref()
> function with keyword arguments) to explicitly specify the join
> conditions. Nested error is "Cant determine join between 'products'
> and
> 'collections_products'; tables have more than one foreign key
> constraint
> relationship between them. Please specify the 'onclause' of this join
> explicitly."
>
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.
- mike
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---