Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How works count subquery? #2118

Closed
EugeneRymarev opened this issue Mar 1, 2020 · 1 comment
Closed

How works count subquery? #2118

EugeneRymarev opened this issue Mar 1, 2020 · 1 comment

Comments

@EugeneRymarev
Copy link

I need count in subquery. Example at http://docs.peewee-orm.com/en/latest/peewee/hacks.html#other-methods
My code:

subquery_condition = (UserSurvey.user == User.id) & (UserSurvey.is_current == False)
subquery = (UserSurvey.select(fn.COUNT(UserSurvey.id)).where(subquery_condition))
condition = User.next_notify & (User.next_notify <= dt_now) & (subquery == 0)

I receive error:

Traceback (most recent call last):
File "/mnt/d/projects/venvs/survey_bot/lib/python3.8/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/mnt/d/projects/venvs/survey_bot/lib/python3.8/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/mnt/d/projects/venvs/survey_bot/lib/python3.8/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/mnt/d/projects/venvs/survey_bot/lib/python3.8/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/mnt/d/projects/survey_bot/project/tlgrm.py", line 37, in notifications
dialog_bot.send_notifications()
File "/mnt/d/projects/survey_bot/project/telegram_dialog/bot.py", line 280, in send_notifications
users = get_user_for_notifications()
File "/mnt/d/projects/survey_bot/project/views/user.py", line 103, in get_user_for_notifications
condition = User.next_notify & (User.next_notify <= dt_now) & (subquery == 0)
File "/mnt/d/projects/venvs/survey_bot/lib/python3.8/site-packages/peewee.py", line 817, in eq
return self._hash == other._hash
AttributeError: 'int' object has no attribute '_hash'

Process finished with exit code 1

@coleifer
Copy link
Owner

coleifer commented Mar 1, 2020

Fixed. As a workaround until I tag a new release, you can use:

Expression(subquery, OP.EQ, 0)

Instead of subquery == 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants