On Dec 28, 2008, at 9:35 PM, Alan Shields wrote:

>
> On Sun, Dec 28, 2008 at 9:29 AM, Michael Bayer <mike...@zzzcomputing.com 
> > wrote:
>> you'd put
>> "primaryjoin=crate_table.c.crate_id==crates2apples.c.some_column"
>> inside of the relation().    This approach usually makes the
>> eagerload's job easier, as far as constructing a join from  
>> crate_table
>> to Apple.
>>
>> Of course the only way to know the best approach is to know exactly
>> what data you need to be loading.    Its often the case that the best
>> approach overall is to use just a plain Crate->Apple relation with
>> nothing special, then just use a simple in-python property to give  
>> you
>> a particular "view" of that collection.   If you're dealing with  
>> small
>> numbers of records that's how I'd usually do something like this.  I
>> think the docs try to make a similar point on this.
>
> Several thousand records, sadly. I will probably be able to get away
> with the in-python property for
> most cases, but this is the ugly one.
>
> So, I gave that a shot and I must have messed up somewhere.
>
> crates2apples = \
>    select([ crate_table.c.id.label("crate_id"),
>             apple_table,
>             ],
>           from_obj = crate_table.\
>               <bunch of joins>.outerjoin(apple_table, <condition>))
> crates2apples_a = crates2apples.alias('_crates2apples')
>
> mapper(Apple, crates2apples_a, non_primary=True)
>
> mapper(Crate, crate_table, properties={
>        'apples': relation(Apple, lazy=True, uselist=True,  
> viewonly=True,
>                                   primaryjoin= crate_table.c.id ==
> crates2apples_a.c.crate_id,
>                                    ),
>        })

you need to map to the non_primary mapper.

npapple = mapper(Apple, crates2apples_a, non_primary=True)

mapper(Crate, table, properties={
      'apples':relation(npapple, ...)
})



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to