Fixed #32252 -- Fixed __isnull=True on key transforms on SQLite and Oracle.
__isnull=True on key transforms should not match keys with NULL values.
This commit is contained in:
parent
3f140dde51
commit
8d7085e0fd
|
@ -366,14 +366,25 @@ class CaseInsensitiveMixin:
|
|||
class KeyTransformIsNull(lookups.IsNull):
|
||||
# key__isnull=False is the same as has_key='key'
|
||||
def as_oracle(self, compiler, connection):
|
||||
sql, params = HasKey(
|
||||
self.lhs.lhs,
|
||||
self.lhs.key_name,
|
||||
).as_oracle(compiler, connection)
|
||||
if not self.rhs:
|
||||
return HasKey(self.lhs.lhs, self.lhs.key_name).as_oracle(compiler, connection)
|
||||
return super().as_sql(compiler, connection)
|
||||
return sql, params
|
||||
# Column doesn't have a key or IS NULL.
|
||||
lhs, lhs_params, _ = self.lhs.preprocess_lhs(compiler, connection)
|
||||
return '(NOT %s OR %s IS NULL)' % (sql, lhs), tuple(params) + tuple(lhs_params)
|
||||
|
||||
def as_sqlite(self, compiler, connection):
|
||||
template = 'JSON_TYPE(%s, %%s) IS NULL'
|
||||
if not self.rhs:
|
||||
return HasKey(self.lhs.lhs, self.lhs.key_name).as_sqlite(compiler, connection)
|
||||
return super().as_sql(compiler, connection)
|
||||
template = 'JSON_TYPE(%s, %%s) IS NOT NULL'
|
||||
return HasKey(self.lhs.lhs, self.lhs.key_name).as_sql(
|
||||
compiler,
|
||||
connection,
|
||||
template=template,
|
||||
)
|
||||
|
||||
|
||||
class KeyTransformIn(lookups.In):
|
||||
|
|
|
@ -9,4 +9,6 @@ Django 3.1.5 fixes several bugs in 3.1.4.
|
|||
Bugfixes
|
||||
========
|
||||
|
||||
* ...
|
||||
* Fixed ``__isnull=True`` lookup on key transforms for
|
||||
:class:`~django.db.models.JSONField` with Oracle and SQLite
|
||||
(:ticket:`32252`).
|
||||
|
|
|
@ -586,6 +586,10 @@ class TestQuerying(TestCase):
|
|||
NullableJSONModel.objects.filter(value__a__isnull=True),
|
||||
self.objs[:3] + self.objs[5:],
|
||||
)
|
||||
self.assertSequenceEqual(
|
||||
NullableJSONModel.objects.filter(value__j__isnull=True),
|
||||
self.objs[:4] + self.objs[5:],
|
||||
)
|
||||
self.assertSequenceEqual(
|
||||
NullableJSONModel.objects.filter(value__a__isnull=False),
|
||||
[self.objs[3], self.objs[4]],
|
||||
|
|
Loading…
Reference in New Issue