With the following schema:

Table(u'account', meta,
 Column(u'id', Integer(), primary_key=True, nullable=False),
 Column(u'name', String(length=5, convert_unicode=False,
assert_unicode=None), nullable=False))

Table(u'license', meta,
 Column(u'id', Integer(), primary_key=True, nullable=False),
 Column(u'accountid', Integer(), ForeignKey(u'account.id')))

and the following mapper configuration:

mapper(Account, meta.tables['account'],
  properties = {
  'licenses': sa_orm.relation(
    License,
    lazy=True,
    backref='account',
    uselist=True, # probably redundant
  ),
  },
  **mapper_kwargs
)
mapper(License, meta.tables['license'], **mapper_kwargs)


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.



produces the following output:

SELECT account.id AS account_id, account.name AS account_name
FROM account ORDER BY account.id

SELECT account.id AS account_id, account.name AS account_name
FROM account
WHERE EXISTS (SELECT 1
FROM license
WHERE account.id = license.accountid) ORDER BY account.id

SELECT account.id AS account_id, account.name AS account_name
FROM account
WHERE NOT (EXISTS (SELECT 1
FROM license
WHERE account.id = license.accountid)) ORDER BY account.id

[]



The last two queries produce the desired results but the first
doesn't, but it doesn't generate any errors, either. The first query
is what I originally wrote as it is what I simply transcribed from
what I was thinking:

Query for Account objects for which the licenses attribute is an empty
list.

accts = s.query(Account).filter(Account.licenses==[])

I'm using 0.4.6 and PostgreSQL 8.3.1

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to