Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

2 participants

@errx

Assertion that query hasn't been sliced when calling exists() method.

@apollo13
Owner

Can you open a new pull request with the comments from the ticket? Thx!

@apollo13 apollo13 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 18, 2012
This page is out of date. Refresh to see the latest.
View
2  django/db/models/query.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)
View
9 tests/regressiontests/queries/tests.py
@@ -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
Something went wrong with that request. Please try again.