On Tue, Jul 15, 2008 at 9:31 AM, Michael Bayer <[EMAIL PROTECTED]> wrote: > > > 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).
Thanks for the response! It would appear that I'm guilty of "thinking in Python" not "thinking in SQL" for this one. I might make a note that I made a mistake in my post - the last two queries are *not* the same. The comparison to None is equiv. to ~Account.licenses.any() - what I have above is missing the '~'. -- Jon --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
