question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unwanted 'AS' when constructing HAVING clause

See original GitHub issue

Hi!

When I use my peewee.Func object, which has _alias, in having(…), then Peewee adds keyword ‘AS’ in clause.

For example:

field = fn.Count(SystemUser.id).alias('roles_count')
query = query.having(field == 2)

Then I get syntax error:

…name" HAVING ((Count(“t1”.“id”) AS roles_count…

I construct queries automatically from rest-requests, so I need to take fields from query.get_query_meta()[0] for comparing with requests and constructing query.

It is easy just clean _alias before constructing query and problem will go away, but nevertheless I think that it is strange behavior.

Thanks for attention!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Aug 1, 2017

This “problem” has been present in peewee since at least version 2.0.0, and at present, exists in the 3.0 rewrite (which you can see here).

The typical way to handle this is to either:

  1. Store the unaliased SQL entity in a variable and only call alias() when you need to.
  2. Use SQL() object to reference the alias directly.

Example 1:

# Sort users by number of tweets composed, when that number is less than 10
count = fn.COUNT(Tweet.id)
query = (User
        .select(User, count.alias('tweet_count'))
        .join(Tweet, JOIN.LEFT_OUTER)
        .group_by(User)
        .having(count < 10)
        .order_by(count))

Example 2:

# Sort users by number of tweets composed, when that number is less than 10
count = fn.COUNT(Tweet.id).alias('tweet_count')
count_ref = SQL('tweet_count')
query = (User
        .select(User, count.alias('tweet_count'))
        .join(Tweet, JOIN.LEFT_OUTER)
        .group_by(User)
        .having(count_ref < 10)
        .order_by(count_ref))
0reactions
coleifercommented, Aug 25, 2017

This is fixed by e88dbf5145261890bc1a55a2f24ce6dfba36d8db in the 3.0a branch.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ignore Field used in HAVING clause on SELECT
The easiest way is to use a simple subquery: SELECT id, name, -- midpointlevel, <-- commented out, since we don't need this column...
Read more >
4. Filtering - Learning SQL, 3rd Edition [Book] - O'Reilly
This chapter explores the various types of filter conditions that you can employ in the where clauses of select , update , and...
Read more >
7 Common GROUP BY Errors - LearnSQL.com
1. Forgetting GROUP BY with Aggregate Functions ... You use SELECT statements with the GROUP BY clause when you want to group and...
Read more >
mysql - Query with HAVING clause is taking too long
WHERE is done while gathering rows; HAVING is done after all the rows are gathered. That is, WHERE is more efficient. However, HAVING...
Read more >
SQL HAVING Clause - W3Schools
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions. HAVING Syntax. SELECT column_name(s) FROM table_name...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found