If I try that, I get:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) aggregates not
allowed in WHERE clause

The SQL generated is:

SELECT account.accountid AS account_accountid
FROM account
JOIN userinfo ON account.accountid = userinfo.accountid
WHERE count(userinfo.userid) > %(count_1)s
GROUP BY account.accountid

What I'd like is the SA translation of:

SELECT account.accountid, count(users.userid)
FROM account
JOIN users ON account.accountid = users.accountid
GROUP BY account.accountid
HAVING count(users.userid) > 1;

This works:
sess.query(Account.accountid).
  join(Account.users).
  group_by(Account.accountid).
  having(sa.func.count(User.userid)>1).all()

but it returns an int (I want the full Account object).

However, starting with:
sess.query(Account)
does not because a bunch of the columns loaded as part of Account are
not automatically added to the group by.

This leads me to a possible bug report regarding subquery and .description:

I wanted to start with this:

q0 = s.query(Account.accountid, sa.func.count(User.userid).label('user_count'))
      .join(Account.users)
      .group_by(Account.accountid)
      .having(sa.func.count(User.userid)>1)
      .subquery()

and build on it, but

s.query(Account).join(q0).all()

results in:

sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
between 'account' and '%(20357008 anon)s'

and thus, a possible bug:

>>> q0.description
'%(20357008 anon)s'
>>>



-- 
Jon

--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to