On Jun 11, 2007, at 12:21 PM, Glauco wrote:
> For this reason i've inserted explicit primaryjoin condition, and
> these condition work perfectly in the primary mapper.
> But in the secondary wont work...
>
theres many reasons why you probably dont want to do what youre
trying to do.
first off, adding relations to a non-primary mapper: the class still
will lazy load relationships from the primary mapper, since they are
invoked from attributes. it doesnt entirely make sense to add
relationships to a secondary mapper for this reason - the
relationships will only take effect from the secondary mapper if you
are using eager loading. and in that case, we dont really have any
test cases for that so i cant say how well its going to work.
secondly: a non-primary mapper is just an awkward way of defining an
ORM query. Since we support generative queries now, you can just
make a Query object with the criterion youre looking for and just
hold onto it...youre just adding a single WHERE criterion, so
my_secondary_query = query(MyClass).filter(table.c.tipo=='p'). much
easier. ideally Query should be replacing 90% of all non_primary
mapper use cases.
thirdly: it doesnt make much sense to use assign_mapper() with a
secondary mapper, since thats not the mapper which the class is
primarily associated with.
So after all that, if you still want to get it to work, you need the
primaryjoin criterion to be against the mapped selectable:
myselect = select[mytable]).alias('myselect')
mapper(MyClass, myselect, non_primary=True, properties={
'stuff': relation(OtherClass,
primaryjoin=myselect.c.id==othertable.c.myid)
})
you also may need to add the foreign_keys=[<cols>] parameter which
will identfify which columns in the join conditions are "foreign", if
SA cannot figure out what they are.
mapper(MyClass, myselect, non_primary=True, properties={
'stuff': relation(OtherClass,
primaryjoin=myselect.c.id==othertable.c.myid, foreign_keys=
[othertable.c.myid])
})
hope this helps...good luck
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---