On Jul 14, 2008, at 7:19 PM, Jon wrote:

> I've encountered some weird stuff. I'm probably doing it wrong, but I
> don't understand *why*.
>
> The following code:
>
>
> s = Session()
> # returns all accounts, the .licenses param
> # not taken into consideration
> accts = s.query(Account).filter(Account.licenses==[])
> print accts
> print
>
> accts = s.query(Account).filter(Account.licenses.any())
> print accts
> print
>
> # returns the correct list of accounts.
> accts = s.query(Account).filter(Account.licenses==None)
> print accts
> print
>
> print accts[0].licenses # prints [] not None.

Comparison to lists is currently taken to mean "return objects whos  
collection contains these elements", i.e. its like a mass  
Account.licenses.contains() operator.  So if you compare to an empty  
list that would return all parent elements.  Comparison to None  
returns items using a "NOT EXISTS" clause, i.e. parent items with no  
child items.  any() returns items where any of them meet the given  
criterion -since the criteiron is blank here, all child items match.    
any() is the best operator to use when asking for parents with no  
child items, using ~Account.licenses.any() (I think theres a tutorial  
example to this effect).

Once loaded, all collection-based attributes default to the empty  
collection if no elements are present, which is by default a list. 
  

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to