Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #18414 -- qs.exists() for sliced distinct queries

  • Loading branch information...
commit 93cc6dcdac6fc3e506640fa38dd1798c3cd61cff 1 parent 068b1e0
Anssi Kääriäinen akaariai authored
Showing with 21 additions and 1 deletion.
  1. +2 −1  django/db/models/sql/query.py
  2. +19 −0 tests/queries/tests.py
3  django/db/models/sql/query.py
View
@@ -431,7 +431,8 @@ def get_count(self, using):
def has_results(self, using):
q = self.clone()
- q.clear_select_clause()
+ if not q.distinct:
+ q.clear_select_clause()
q.clear_ordering(True)
q.set_limits(high=1)
compiler = q.get_compiler(using=using)
19 tests/queries/tests.py
View
@@ -1856,6 +1856,25 @@ def test_exists(self):
id, name = connection.ops.quote_name('id'), connection.ops.quote_name('name')
self.assertTrue(id not in qstr and name not in qstr)
+ def test_ticket_18414(self):
+ Article.objects.create(name='one', created=datetime.datetime.now())
+ Article.objects.create(name='one', created=datetime.datetime.now())
+ Article.objects.create(name='two', created=datetime.datetime.now())
+ self.assertTrue(Article.objects.exists())
+ self.assertTrue(Article.objects.distinct().exists())
+ self.assertTrue(Article.objects.distinct()[1:3].exists())
+ self.assertFalse(Article.objects.distinct()[1:1].exists())
+
+ @unittest.skipUnless(connection.features.can_distinct_on_fields,
+ 'Uses distinct(fields)')
+ def test_ticket_18414_distinct_on(self):
+ Article.objects.create(name='one', created=datetime.datetime.now())
+ Article.objects.create(name='one', created=datetime.datetime.now())
+ Article.objects.create(name='two', created=datetime.datetime.now())
+ self.assertTrue(Article.objects.distinct('name').exists())
+ self.assertTrue(Article.objects.distinct('name')[1:2].exists())
+ self.assertFalse(Article.objects.distinct('name')[2:3].exists())
+
class QuerysetOrderedTests(unittest.TestCase):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.