{"action":"created","comment":{"url":"https://api.github.com/repos/django/django/pulls/comments/25856205","id":25856205,"diff_hunk":"@@ -390,10 +390,19 @@ def get_aggregation(self, using, added_aggregate_names):\n             from django.db.models.sql.subqueries import AggregateQuery\n             outer_query = AggregateQuery(self.model)\n             inner_query = self.clone()\n-            if not has_limit and not self.distinct_fields:\n-                inner_query.clear_ordering(True)\n             inner_query.select_for_update = False\n             inner_query.select_related = False\n+            if not has_limit and not self.distinct_fields:\n+                inner_query.clear_ordering(True)\n+            if not inner_query.distinct:\n+                # If the inner query uses default select, and it has some aggregate annotations,\n+                # then we must make sure the inner query is grouped by the main model's primary\n+                # key. However, clearing the select clause can alter results in case of distinct","path":"django/db/models/sql/query.py","position":22,"original_position":22,"commit_id":"4436c7dac74420711fd6783c34efda8b458acea9","original_commit_id":"4436c7dac74420711fd6783c34efda8b458acea9","user":{"login":"timgraham","id":411869,"avatar_url":"https://avatars.githubusercontent.com/u/411869?v=3","gravatar_id":"","url":"https://api.github.com/users/timgraham","html_url":"https://github.com/timgraham","followers_url":"https://api.github.com/users/timgraham/followers","following_url":"https://api.github.com/users/timgraham/following{/other_user}","gists_url":"https://api.github.com/users/timgraham/gists{/gist_id}","starred_url":"https://api.github.com/users/timgraham/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timgraham/subscriptions","organizations_url":"https://api.github.com/users/timgraham/orgs","repos_url":"https://api.github.com/users/timgraham/repos","events_url":"https://api.github.com/users/timgraham/events{/privacy}","received_events_url":"https://api.github.com/users/timgraham/received_events","type":"User","site_admin":false},"body":"in case of -> if","created_at":"2015-03-05T11:16:14Z","updated_at":"2015-03-05T11:16:14Z","html_url":"https://github.com/django/django/pull/4240#discussion_r25856205","pull_request_url":"https://api.github.com/repos/django/django/pulls/4240","_links":{"self":{"href":"https://api.github.com/repos/django/django/pulls/comments/25856205"},"html":{"href":"https://github.com/django/django/pull/4240#discussion_r25856205"},"pull_request":{"href":"https://api.github.com/repos/django/django/pulls/4240"}}},"pull_request":{"url":"https://api.github.com/repos/django/django/pulls/4240","id":30536735,"html_url":"https://github.com/django/django/pull/4240","diff_url":"https://github.com/django/django/pull/4240.diff","patch_url":"https://github.com/django/django/pull/4240.patch","issue_url":"https://api.github.com/repos/django/django/issues/4240","number":4240,"state":"open","locked":false,"title":"Fixed #24171 -- failure with complex aggregate query and expressions","user":{"login":"akaariai","id":125285,"avatar_url":"https://avatars.githubusercontent.com/u/125285?v=3","gravatar_id":"","url":"https://api.github.com/users/akaariai","html_url":"https://github.com/akaariai","followers_url":"https://api.github.com/users/akaariai/followers","following_url":"https://api.github.com/users/akaariai/following{/other_user}","gists_url":"https://api.github.com/users/akaariai/gists{/gist_id}","starred_url":"https://api.github.com/users/akaariai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akaariai/subscriptions","organizations_url":"https://api.github.com/users/akaariai/orgs","repos_url":"https://api.github.com/users/akaariai/repos","events_url":"https://api.github.com/users/akaariai/events{/privacy}","received_events_url":"https://api.github.com/users/akaariai/received_events","type":"User","site_admin":false},"body":"The query used a construct of qs.annotate().values().aggregate() where\r\nthe first annotate used an F-object reference, and the .values() and\r\n.aggregate() calls referenced that F-object.\r\n\r\nAlso made sure the inner query's select clause is as simple as possible,\r\nand made sure .values().distinct().aggreate() works correctly.","created_at":"2015-03-05T07:56:33Z","updated_at":"2015-03-05T11:16:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/django/django/pulls/4240/commits","review_comments_url":"https://api.github.com/repos/django/django/pulls/4240/comments","review_comment_url":"https://api.github.com/repos/django/django/pulls/comments{/number}","comments_url":"https://api.github.com/repos/django/django/issues/4240/comments","statuses_url":"https://api.github.com/repos/django/django/statuses/4436c7dac74420711fd6783c34efda8b458acea9","head":{"label":"akaariai:ticket_24171","ref":"ticket_24171","sha":"4436c7dac74420711fd6783c34efda8b458acea9","user":{"login":"akaariai","id":125285,"avatar_url":"https://avatars.githubusercontent.com/u/125285?v=3","gravatar_id":"","url":"https://api.github.com/users/akaariai","html_url":"https://github.com/akaariai","followers_url":"https://api.github.com/users/akaariai/followers","following_url":"https://api.github.com/users/akaariai/following{/other_user}","gists_url":"https://api.github.com/users/akaariai/gists{/gist_id}","starred_url":"https://api.github.com/users/akaariai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akaariai/subscriptions","organizations_url":"https://api.github.com/users/akaariai/orgs","repos_url":"https://api.github.com/users/akaariai/repos","events_url":"https://api.github.com/users/akaariai/events{/privacy}","received_events_url":"https://api.github.com/users/akaariai/received_events","type":"User","site_admin":false},"repo":{"id":4170732,"name":"django","full_name":"akaariai/django","owner":{"login":"akaariai","id":125285,"avatar_url":"https://avatars.githubusercontent.com/u/125285?v=3","gravatar_id":"","url":"https://api.github.com/users/akaariai","html_url":"https://github.com/akaariai","followers_url":"https://api.github.com/users/akaariai/followers","following_url":"https://api.github.com/users/akaariai/following{/other_user}","gists_url":"https://api.github.com/users/akaariai/gists{/gist_id}","starred_url":"https://api.github.com/users/akaariai/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akaariai/subscriptions","organizations_url":"https://api.github.com/users/akaariai/orgs","repos_url":"https://api.github.com/users/akaariai/repos","events_url":"https://api.github.com/users/akaariai/events{/privacy}","received_events_url":"https://api.github.com/users/akaariai/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/akaariai/django","description":"The Web framework for perfectionists with deadlines. Now on GitHub.","fork":true,"url":"https://api.github.com/repos/akaariai/django","forks_url":"https://api.github.com/repos/akaariai/django/forks","keys_url":"https://api.github.com/repos/akaariai/django/keys{/key_id}","collaborators_url":"https://api.github.com/repos/akaariai/django/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/akaariai/django/teams","hooks_url":"https://api.github.com/repos/akaariai/django/hooks","issue_events_url":"https://api.github.com/repos/akaariai/django/issues/events{/number}","events_url":"https://api.github.com/repos/akaariai/django/events","assignees_url":"https://api.github.com/repos/akaariai/django/assignees{/user}","branches_url":"https://api.github.com/repos/akaariai/django/branches{/branch}","tags_url":"https://api.github.com/repos/akaariai/django/tags","blobs_url":"https://api.github.com/repos/akaariai/django/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/akaariai/django/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/akaariai/django/git/refs{/sha}","trees_url":"https://api.github.com/repos/akaariai/django/git/trees{/sha}","statuses_url":"https://api.github.com/repos/akaariai/django/statuses/{sha}","languages_url":"https://api.github.com/repos/akaariai/django/languages","stargazers_url":"https://api.github.com/repos/akaariai/django/stargazers","contributors_url":"https://api.github.com/repos/akaariai/django/contributors","subscribers_url":"https://api.github.com/repos/akaariai/django/subscribers","subscription_url":"https://api.github.com/repos/akaariai/django/subscription","commits_url":"https://api.github.com/repos/akaariai/django/commits{/sha}","git_commits_url":"https://api.github.com/repos/akaariai/django/git/commits{/sha}","comments_url":"https://api.github.com/repos/akaariai/django/comments{/number}","issue_comment_url":"https://api.github.com/repos/akaariai/django/issues/comments{/number}","contents_url":"https://api.github.com/repos/akaariai/django/contents/{+path}","compare_url":"https://api.github.com/repos/akaariai/django/compare/{base}...{head}","merges_url":"https://api.github.com/repos/akaariai/django/merges","archive_url":"https://api.github.com/repos/akaariai/django/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/akaariai/django/downloads","issues_url":"https://api.github.com/repos/akaariai/django/issues{/number}","pulls_url":"https://api.github.com/repos/akaariai/django/pulls{/number}","milestones_url":"https://api.github.com/repos/akaariai/django/milestones{/number}","notifications_url":"https://api.github.com/repos/akaariai/django/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/akaariai/django/labels{/name}","releases_url":"https://api.github.com/repos/akaariai/django/releases{/id}","created_at":"2012-04-28T22:21:32Z","updated_at":"2014-06-14T10:49:41Z","pushed_at":"2015-03-05T10:56:07Z","git_url":"git://github.com/akaariai/django.git","ssh_url":"git@github.com:akaariai/django.git","clone_url":"https://github.com/akaariai/django.git","svn_url":"https://github.com/akaariai/django","homepage":"http://www.djangoproject.com/","size":108048,"stargazers_count":11,"watchers_count":11,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":0,"forks":3,"open_issues":0,"watchers":11,"default_branch":"master"}},"base":{"label":"django:master","ref":"master","sha":"39a7eed1bbf12020a077e4bec3d82e08f171a021","user":{"login":"django","id":27804,"avatar_url":"https://avatars.githubusercontent.com/u/27804?v=3","gravatar_id":"","url":"https://api.github.com/users/django","html_url":"https://github.com/django","followers_url":"https://api.github.com/users/django/followers","following_url":"https://api.github.com/users/django/following{/other_user}","gists_url":"https://api.github.com/users/django/gists{/gist_id}","starred_url":"https://api.github.com/users/django/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/django/subscriptions","organizations_url":"https://api.github.com/users/django/orgs","repos_url":"https://api.github.com/users/django/repos","events_url":"https://api.github.com/users/django/events{/privacy}","received_events_url":"https://api.github.com/users/django/received_events","type":"Organization","site_admin":false},"repo":{"id":4164482,"name":"django","full_name":"django/django","owner":{"login":"django","id":27804,"avatar_url":"https://avatars.githubusercontent.com/u/27804?v=3","gravatar_id":"","url":"https://api.github.com/users/django","html_url":"https://github.com/django","followers_url":"https://api.github.com/users/django/followers","following_url":"https://api.github.com/users/django/following{/other_user}","gists_url":"https://api.github.com/users/django/gists{/gist_id}","starred_url":"https://api.github.com/users/django/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/django/subscriptions","organizations_url":"https://api.github.com/users/django/orgs","repos_url":"https://api.github.com/users/django/repos","events_url":"https://api.github.com/users/django/events{/privacy}","received_events_url":"https://api.github.com/users/django/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/django/django","description":"The Web framework for perfectionists with deadlines.","fork":false,"url":"https://api.github.com/repos/django/django","forks_url":"https://api.github.com/repos/django/django/forks","keys_url":"https://api.github.com/repos/django/django/keys{/key_id}","collaborators_url":"https://api.github.com/repos/django/django/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/django/django/teams","hooks_url":"https://api.github.com/repos/django/django/hooks","issue_events_url":"https://api.github.com/repos/django/django/issues/events{/number}","events_url":"https://api.github.com/repos/django/django/events","assignees_url":"https://api.github.com/repos/django/django/assignees{/user}","branches_url":"https://api.github.com/repos/django/django/branches{/branch}","tags_url":"https://api.github.com/repos/django/django/tags","blobs_url":"https://api.github.com/repos/django/django/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/django/django/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/django/django/git/refs{/sha}","trees_url":"https://api.github.com/repos/django/django/git/trees{/sha}","statuses_url":"https://api.github.com/repos/django/django/statuses/{sha}","languages_url":"https://api.github.com/repos/django/django/languages","stargazers_url":"https://api.github.com/repos/django/django/stargazers","contributors_url":"https://api.github.com/repos/django/django/contributors","subscribers_url":"https://api.github.com/repos/django/django/subscribers","subscription_url":"https://api.github.com/repos/django/django/subscription","commits_url":"https://api.github.com/repos/django/django/commits{/sha}","git_commits_url":"https://api.github.com/repos/django/django/git/commits{/sha}","comments_url":"https://api.github.com/repos/django/django/comments{/number}","issue_comment_url":"https://api.github.com/repos/django/django/issues/comments{/number}","contents_url":"https://api.github.com/repos/django/django/contents/{+path}","compare_url":"https://api.github.com/repos/django/django/compare/{base}...{head}","merges_url":"https://api.github.com/repos/django/django/merges","archive_url":"https://api.github.com/repos/django/django/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/django/django/downloads","issues_url":"https://api.github.com/repos/django/django/issues{/number}","pulls_url":"https://api.github.com/repos/django/django/pulls{/number}","milestones_url":"https://api.github.com/repos/django/django/milestones{/number}","notifications_url":"https://api.github.com/repos/django/django/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/django/django/labels{/name}","releases_url":"https://api.github.com/repos/django/django/releases{/id}","created_at":"2012-04-28T02:47:18Z","updated_at":"2015-03-05T11:07:50Z","pushed_at":"2015-03-05T09:11:08Z","git_url":"git://github.com/django/django.git","ssh_url":"git@github.com:django/django.git","clone_url":"https://github.com/django/django.git","svn_url":"https://github.com/django/django","homepage":"https://www.djangoproject.com/","size":263318,"stargazers_count":13154,"watchers_count":13154,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5131,"mirror_url":null,"open_issues_count":94,"forks":5131,"open_issues":94,"watchers":13154,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/django/django/pulls/4240"},"html":{"href":"https://github.com/django/django/pull/4240"},"issue":{"href":"https://api.github.com/repos/django/django/issues/4240"},"comments":{"href":"https://api.github.com/repos/django/django/issues/4240/comments"},"review_comments":{"href":"https://api.github.com/repos/django/django/pulls/4240/comments"},"review_comment":{"href":"https://api.github.com/repos/django/django/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/django/django/pulls/4240/commits"},"statuses":{"href":"https://api.github.com/repos/django/django/statuses/4436c7dac74420711fd6783c34efda8b458acea9"}}}}