Permalink
Browse files

Fixed #18414 -- Raise assertion with exists() and sliced query

  • Loading branch information...
1 parent aee9eec commit 3a2a48f2fe5f4110e59b745e691d40afb731b5de Oleg Matrokhin committed Jun 18, 2012
Showing with 11 additions and 0 deletions.
  1. +2 −0 django/db/models/query.py
  2. +9 −0 tests/regressiontests/queries/tests.py
@@ -569,6 +569,8 @@ def _update(self, values):
_update.alters_data = True
def exists(self):
+ assert self.query.can_filter(),\
+ "Cannot check existance once a slice has been taken."
if self._result_cache is None:
return self.query.has_results(using=self.db)
return bool(self._result_cache)
@@ -1580,12 +1580,21 @@ def test_ticket8597(self):
class ExistsSql(TestCase):
def setUp(self):
settings.DEBUG = True
+ Article.objects.create(name='one', created=datetime.datetime.now())
def test_exists(self):
self.assertFalse(Tag.objects.exists())
# Ok - so the exist query worked - but did it include too many columns?
self.assertTrue("id" not in connection.queries[-1]['sql'] and "name" not in connection.queries[-1]['sql'])
+ def test_ticket_18414(self):
+ self.assertEqual(Article.objects.all().exists(), True)
+ self.assertRaisesMessage(
+ AssertionError,
+ 'Cannot check existance once a slice has been taken.',
+ Article.objects.all()[:1].exists
+ )
+
def tearDown(self):
settings.DEBUG = False

0 comments on commit 3a2a48f

Please sign in to comment.