2 messages in com.googlegroups.sqlalchemy[sqlalchemy] Query for an empty to_ma...
FromSent OnAttachments
Gaetan de Menten25 Feb 2008 04:45 
Michael Bayer25 Feb 2008 08:05 
Subject:[sqlalchemy] Query for an empty to_many relationship?
From:Gaetan de Menten (gdem@gmail.com)
Date:02/25/2008 04:45:14 AM
List:com.googlegroups.sqlalchemy

Hello all,

I'm puzzled as I cannot seem to do a pretty simple query: I have a many to many relationship (say from Vendor to Item) and want to get a list of all vendors which have no item.

The obvious: Vendor.query.filter(Vendor.items == []).all() does not work (it produce a query without any where clause).

Also, Vendor.items.count doesn't exist. It would be nice if we could express things like: Vendor.query.filter(Vendor.items.count() == 0).all()

Maybe I'm just not awake yet, but can someone enlighten me how to do that?

On a related note, I've seen the following block in the documentation (in the Relation "Operators" section):

# locate an address sql>>> address = session.query(Address).\ ... filter(Address.email_address=='ja@google.com').one()

['ja@google.com']

# use the address in a filter_by expression sql>>> session.query(User).filter_by(addresses=address).all()

Is it a simple mistake in the docs or is it really valid? In the later case, shouldn't [collection attribute == single instance] be an invalid case since the new "contains", "has" and "any" operators appeared? Or is filter_by(x=y) not always equal to CurrentJoinPoint.x == y ?