#31231: ORM doesn't find a row with a SQL null JSONB field.
-------------------------------------+-------------------------------------
               Reporter:  mvarnar    |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  JSON, ORM, NULL
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 If you create a row with a nullable jsonb field and then try to find the
 row with a null value, you won't find any row, even if they exist. It's
 because orm looks for json 'null', not sql 'null'.

 Steps to reproduce:

 1. Create a project with models.py and test.py as described bellow.


 {{{
 # models.py
 from django.db import models
 from django.contrib.postgres.fields import JSONField


 class TestModel(models.Model):
     json_field = JSONField(null=True)
 }}}


 {{{
 # test.py
 from django.test import TestCase

 from test_app.models import TestModel


 class TestJsonbNull(TestCase):
     def setUp(self):
         TestModel.objects.create(json_field=None)

     def test_row_has_jsonb_null_field(self):
         obj = TestModel.objects.values().all()[0]
         self.assertIsNone(obj['json_field'])

     def test_row_with_jsonb_null_field_exists(self):
         print(TestModel.objects.filter(json_field=None).query)
 self.assertTrue(TestModel.objects.filter(json_field=None).exists())

 }}}

 2. Run {{{ python manage.py test }}}

 Expected behaviour:
 Both tests will pass

 Actual behaviour:
 {{{ test_row_with_jsonb_null_field_exists }}} will fail.

 Checked for:
 python==3.6.9
 Django==3.0.3
   - asgiref==3.2.3
   - pytz==2019.3
   - sqlparse== 0.3.0
 psycopg2==2.8.4

 Database is postgresql 11.6

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31231>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.6b51a0d972e1425f472764e5daccb8fa%40djangoproject.com.

Reply via email to