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

Fixed #24835 - exists() incorrect after annotation with Count() #4706

Closed
wants to merge 1 commit into from

Conversation

pwmarcz
Copy link
Contributor

@pwmarcz pwmarcz commented May 24, 2015

https://code.djangoproject.com/ticket/24835

I followed the same approach as in QuerySet._values(). Seems hacky but I couldn't figure out a better way.

@jarshwah
Copy link
Member

I think the approach you've taken is mostly correct. It's unfortunate that similar processing has to be done in so many places, but that's a task for another time.

I feel like the majority of the work should be performed by db.sql.query.has_results rather than db.query.exists. Is there a technical reason for this?

@akaariai
Copy link
Member

I second jarshwah's opinion - the logic should be in has_results(), not in exists(). (Seems like the ._values() logic should also be in sql.Query class, but this doesn't need to be solved here). We could even implement a dedicated helper method to be used both from ._values() and has_results().

@timgraham
Copy link
Member

Please also add 1.8.3 release notes.

@pwmarcz
Copy link
Contributor Author

pwmarcz commented May 25, 2015

I second jarshwah's opinion - the logic should be in has_results(), not in exists().

I moved it to has_results().

Please also add 1.8.3 release notes.

Done.

@@ -9,4 +9,7 @@ Django 1.8.3 fixes several bugs in 1.8.2.
Bugfixes
========

* Fixed `exists()` returning incorrect results after annotation with `Count()`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need double backticks to have an effect on the rendered docs.

@timgraham
Copy link
Member

Could you rebase and squash your commits? Thanks.

QuerySet.exists() incorrectly handled query.group_by = True
case (grouping by all select fields), causing GROUP BY
expressions to be wiped along with select fields.
@pwmarcz
Copy link
Contributor Author

pwmarcz commented May 25, 2015

Rebased on master and squashed.

As a rule, should I do that with every update or only at the end of review process?

@timgraham
Copy link
Member

For small patches, squashing after each update is fine. For larger patches, it can be helpful to review only the changes.

@jarshwah
Copy link
Member

:shipit: LGTM

@@ -489,6 +489,9 @@ def has_filters(self):
def has_results(self, using):
q = self.clone()
if not q.distinct:
if q.group_by is True:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need is True here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because it can also be "truthy" by containing a list of fields. True means all fields. FWIW I also tested objects.values().annotate().exists() and that was fine before and after this patch.

@jarshwah
Copy link
Member

I've opened a new ticket for cleaning up the _values() method and potential factoring with has_results here: https://code.djangoproject.com/ticket/24854

@timgraham
Copy link
Member

merged in 801a84a, thanks!

@timgraham timgraham closed this May 26, 2015
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

Successfully merging this pull request may close these issues.

4 participants