﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31231	ORM doesn't find a row with a SQL null JSONB field.	mvarnar	nobody	"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"	Bug	closed	contrib.postgres	3.0	Normal	invalid	JSON, ORM, NULL		Unreviewed	0	0	0	0	0	0
