q.group_by().having().count() creates an extra HAVING

Issue #2130 resolved
Former user created an issue

Specific example:

# This is fine:
q = session.query(Foo).group_by(Foo.col2).having(func.count(Foo.col2) > 0)

# This creates an extra HAVING:
q.count()

Resulting SQL:

SELECT count(1) AS count_1 FROM (
  SELECT t.col1 AS t_col1, t.col2 AS t_col2 FROM t GROUP BY t.col2 HAVING count(t.col2) > ?
) AS anon_1 HAVING count(t.col2) > ?

Resulting error:

sqlalchemy.exc.OperationalError: (OperationalError) a GROUP BY clause is required before HAVING u'SELECT count(1) AS count_1 \nFROM (SELECT t.col1 AS t_col1, t.col2 AS t_col2 \nFROM t GROUP BY t.col2 \nHAVING count(t.col2) > ?) AS anon_1 \nHAVING count(t.col2) > ?' (0, 0)

Comments (4)

  1. Mike Bayer repo owner
    • changed milestone to 0.6.7

    oh no that's 0.6.7 too, just do:

    print q.from_self()
    

    count uses from_self() in 0.7.

  2. Log in to comment