summaryrefslogtreecommitdiff
path: root/django/db/models/sql
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2017-04-29 14:53:43 -0400
committerSimon Charette <charette.s@gmail.com>2017-05-11 20:00:57 -0400
commitdaf2bd3efe53cbfc1c9fd00222b8315708023792 (patch)
tree8a511bc9124f8eb7bf7739b54ce52f5d071aef9b /django/db/models/sql
parentbdf192c59357a0d8117f6f34c94fb32a51e7a774 (diff)
Fixed #28107 -- Disabled grouping of selected primary keys for unmanaged models.
The grouping caused an issue with database views as PostgreSQL's query planer isn't smart enough to introspect primary keys through views. Django doesn't support database views but documents that unmanaged models should be used to query them. Thanks powderflask for the detailed report and investigation.
Diffstat (limited to 'django/db/models/sql')
-rw-r--r--django/db/models/sql/compiler.py7
1 files changed, 6 insertions, 1 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index 14a727e998..2543755952 100644
--- a/django/db/models/sql/compiler.py
+++ b/django/db/models/sql/compiler.py
@@ -161,7 +161,12 @@ class SQLCompiler:
# present in the grouped columns. This is done by identifying all
# tables that have their primary key included in the grouped
# columns and removing non-primary key columns referring to them.
- pks = {expr for expr in expressions if hasattr(expr, 'target') and expr.target.primary_key}
+ # Unmanaged models are excluded because they could be representing
+ # database views on which the optimization might not be allowed.
+ pks = {
+ expr for expr in expressions
+ if hasattr(expr, 'target') and expr.target.primary_key and expr.target.model._meta.managed
+ }
aliases = {expr.alias for expr in pks}
expressions = [
expr for expr in expressions if expr in pks or getattr(expr, 'alias', None) not in aliases