On Thursday, February 8, 2018 at 6:23:59 PM UTC-8, Anthony wrote:
>
> Looks like the book is wrong -- according to the code, rows1 & rows2 
> simply combines the records from each Rows object -- it does not find the 
> intersection.
>

Okay, then my results match that!
 

> Anyway, given your code, it looks like we would not expect any results in 
> the intersection of the two Rows objects, as the conditions of each query 
> are mutually exclusive (i.e., "waypoint" either equals X or Y). Of course, 
> if X == Y, then the two sets of Rows are identical, but I assume that isn't 
> generally the case.
>
>
A segment row will only have one waypoint, but multiple segment rows may 
have the same parent run.  I chose to make the run table very simple (and 
in particular, not to have a list of children), but the segment table 
points upward to the run.

If you do really have non-mutually-exclusive results, why not just generate 
> the intersection with the SQL query (i.e., query1 & query2)?
>
>
I'm not very sophisticated at joining in.
 

> Also, note, you queries should use "==", not a single "=".
>
>
erk
 

> Anthony
>
>
/dps
 

> On Tuesday, February 6, 2018 at 4:57:29 PM UTC-5, Dave S wrote:
>>
>> I have an application (used for comparing routes) with 2 tables relevant 
>> to this question.  (Paraphrasing; code is at home,
>> where the router jammed.)
>>
>> db.define_table('run',
>>                        Field('when', 'date'),
>>                        Field('description', 'string))
>>
>> db.define_table('segment',
>>                        Field('waypoint', 'string'),
>>                         Field('timeofday', 'time'),
>>                        Field('odometer', 'decimal'),
>>                        Field('partof', 'reference run'))
>>
>> I wanted to find the runs that had X as a waypoint and also had Y as a 
>> waypoint, so I looked at
>> <URL:
>> http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Combining-rows
>> >
>> and wrote:
>>
>> def fromto():
>>     # not shown -- X and Y from request.vars
>>     q1 = db.segment.waypoint = X
>>     q2 = db.segment.waypoint = Y
>>     runs1 = db(run.id == segment.partof & q1).select(...,distinct=True)
>>     runs2 = db(run.id == segment.partof & q2).select(...,distinct=True)
>>     theruns = runs1 & runs2
>>     print len(runs1) + len(runs2) + len(theruns)
>>     return ("runs" = theruns)
>>
>> For the first waypoints I tried, the print produced counts of 111 10  121
>> which is clearly NOT the intersection.  (I have verified that theruns 
>> includes  runs where Y is not included,
>> and the page shows the Y runs at the end of the X runs (that is, 
>> appended).)
>>
>> If I go for union ('|' in line 7), the counts are 111 10 112.
>> (1 Y run did not go through X).
>>
>> I add the "distinct=True" after the first attempt, in case multiple 
>> appearances of a row was confusing things.   It made no apparent difference.
>>
>> I think this is on 2.14.6; I may be able to test newer web2py's tonight.  
>> Am I doing something wrong, or was there a hiccup in intersection?
>>
>> Dave S
>> /dps
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to