#35833: Annotation yielding an empty set causes entire QuerySet to become empty
-------------------------------------+-------------------------------------
Reporter: Jacob Walls | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):
* cc: Devin Cox (added)
* stage: Unreviewed => Accepted
Comment:
Agree this looks like a bug, here's a rough testy thing in case folks find
it useful
{{{#!diff
--- a/tests/annotations/models.py
+++ b/tests/annotations/models.py
@@ -61,7 +61,7 @@ class Ticket(models.Model):
class JsonModel(models.Model):
- data = models.JSONField(default=dict, blank=True)
+ data = models.JSONField(null=True)
id = models.IntegerField(primary_key=True)
class Meta:
diff --git a/tests/annotations/tests.py b/tests/annotations/tests.py
index 29660a827e..31dfed6ca4 100644
--- a/tests/annotations/tests.py
+++ b/tests/annotations/tests.py
@@ -3,7 +3,7 @@ from decimal import Decimal
from unittest import skipUnless
from django.core.exceptions import FieldDoesNotExist, FieldError
-from django.db import connection
+from django.db import connection, DataError
from django.db.models import (
BooleanField,
Case,
@@ -1188,6 +1188,22 @@ class NonAggregateAnnotationTestCase(TestCase):
self.assertEqual(qs.count(), len(qs))
+ @skipUnless(connection.vendor == "postgresql", "PostgreSQL tests")
+ @skipUnlessDBFeature("supports_json_field")
+ def test_set_returning_functions(self):
+ class JsonbArrayElements(Func):
+ function = "JSONB_ARRAY_ELEMENTS"
+ arity = 1
+ set_returning = True
+
+ JsonModel.objects.create(id=1)
+ qs =
JsonModel.objects.annotate(empty_set=JsonbArrayElements(F("data")))
+ self.assertEqual(qs.count(), 0) # <-- expect to be a DataError
+
+ JsonModel.objects.create(id=2, data={})
+ with self.assertRaises(DataError):
+
JsonModel.objects.annotate(empty_set=JsonbArrayElements(F("data"))).count()
+
class AliasTests(TestCase):
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/35833#comment:1>