Opened 4 years ago

Last modified 4 years ago

#32483 closed Bug

querying JSONField in sqlite returns ints for booleans — at Version 2

Reported by: Matthew Cornell Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: jsonfield querying sqlite
Cc: sage Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Matthew Cornell)

I have a model with a JSONField:

class PredictionData(models.Model):
    data = models.JSONField()

One of the rows contains this dict: {'value': True}.

I'm querying the model's JSON using 'data__value':

PredictionData.objects.values_list('data', 'data__value')

I get correct results for postgres (a boolean) but incorrect for sqlite3 (an int). For this query, sqlite3 wrongly returns:

({'value': True}, 1)

whereas postgres correctly returns

({'value': True}, True)

Same behavior with False/0.

versions:
Python 3.9.1
sqlite3.sqlite_version # '3.33.0'
django.version # '3.1.7'

Change History (2)

comment:1 by Rohith P R, 4 years ago

Triage Stage: UnreviewedAccepted

I was able to reproduce the issue:

from django.db import models


class Messages(models.Model):
    json_field = models.JSONField()
>>> from app.models import Messages
>>> result = Messages.objects.values_list('json_field', 'json_field__is_true')
>>> print(result)
<QuerySet [({'is_true': True}, 1)]>

comment:2 by Matthew Cornell, 4 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top