Oleg Broytmann <phd <at> phd.pp.ru> writes:

> 
> On Mon, Mar 10, 2008 at 08:57:35AM -0700, Daniel Fetchinson wrote:
> > On Mon, Mar 10, 2008 at 3:12 AM, Oleg Broytmann <phd <at> phd.pp.ru> wrote:
> > > On Sun, Mar 09, 2008 at 08:52:28PM -0700, Daniel Fetchinson wrote:
> > > > class zoo( SQLObject ):
> > > >     cages = MultipleJoin( 'cage' )
> > > >
> > > > class cage( SQLObject ):
> > > >     zoo = ForeignKey( 'zoo' )
> > > >     animals = MultipleJoin( 'animal' )
> > > >     properties = RelatedJoin( 'property' )
> > > >
> > > > class animal( SQLObject ):
> > > >     cage = ForeignKey( 'cage' )
> > > >     properties = RelatedJoin( 'property' )
> > > >
> > > > class property( SQLObject ):
> > > >     animals = RelatedJoin( 'animal' )
> > > >     cages = RelatedJoin( 'cage' )
> > > >
> > > >
> > > > What would be the select call for selecting all properties for a given
> > > > zoo instance?
> > >
> > >    SQLObject cannot automagically construct such a comple SQL query. You
> > > can do it in Python:
> > >
> > > z = zoo.get(id)
> > > zoo_properties = []
> > > for c in z.cages:
> > >   for a in c.animals:
> > >      for p in a.properties:
> > >         zoo_properties.append(p)
> > 
> > Thanks, this is the approach I had so far, but I thought some clever join
> > construction would do it if done right.
> 
>    Yes, it is possible, but in a rather complex way. You have to declare
> all intermediate tables for RelatedJoins and manually construct a complex
> join like
> 
> property.select(
>    (property.q.id == property_animal.q.property_id) &&
>    (animal.q.id == property_animal.q.animal_id) &&
>    (animal.q.cage_id == cage.q.id) &&
>    (cage.q.zoo_id == z.id)
> )
> 
> Oleg.

Alternatively, if you switch to using SQLRelatedJoin/SQLMultipleJoin, you can
use SelectResults.throughTo, ie:

cages = cage.select(cage.q.zoo_id == z.id)
a_props = cages.throughTo.animals.throughTo.properties
c_props = cages.throughTo.properties

(Oleg, the SQLBuilder.SQLObjectTable.__getattr__ stuff we pulled out for its bad
interaction with FKs was also targetted at this sort of request, specifically
for not needing to declare the intermediate tables. If someone wants to work
that stuff back in in a way that avoids name conflicts, it would allow your
select above to be written eg:

# Not currently supported, pretend .j is a .q with fk/join attrs for name-safety
props = property.select(property.j.animals && animal.j.cage && (cage.q.zoo_id ==
z.id))

)

- Luke


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to