#31973: TypeError loading data in JSONField if DB has native JSON support
-------------------------------------+-------------------------------------
     Reporter:  Adam Alton           |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  3.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by giacomo):

 * cc: giacomo (added)


Comment:

 I'm also experiencing this error on Django >= 3.1.1 when using
 `jsonb_array_length` on a `JSONField` that contains a list:

 {{{
 class MyModel(models.Model):
     field = JSONField(default=list)

 >>> MyModel.objects.annotate(field_len=Func(F('field'),
 function='jsonb_array_length'))

 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/home/ubuntu/text-annotation/lib/python3.8/site-
 packages/django/db/models/query.py", line 263, in __repr__
     data = list(self[:REPR_OUTPUT_SIZE + 1])
   File "/home/ubuntu/text-annotation/lib/python3.8/site-
 packages/django/db/models/query.py", line 287, in __iter__
     self._fetch_all()
   File "/home/ubuntu/text-annotation/lib/python3.8/site-
 packages/django/db/models/query.py", line 1308, in _fetch_all
     self._result_cache = list(self._iterable_class(self))
   File "/home/ubuntu/text-annotation/lib/python3.8/site-
 packages/django/db/models/query.py", line 70, in __iter__
     for row in compiler.results_iter(results):
   File "/home/ubuntu/text-annotation/lib/python3.8/site-
 packages/django/db/models/sql/compiler.py", line 1100, in apply_converters
     value = converter(value, expression, connection)
   File "/home/ubuntu/text-annotation/lib/python3.8/site-
 packages/django/db/models/fields/json.py", line 83, in from_db_value
     return json.loads(value, cls=self.decoder)
   File
 "/home/ubuntu/.pyenv/versions/3.8.6/lib/python3.8/json/__init__.py", line
 341, in loads
     raise TypeError(f'the JSON object must be str, bytes or bytearray, '
 TypeError: the JSON object must be str, bytes or bytearray, not int
 }}}

 Things that I checked after finding this ticket:
 * the `JSONField` was "converted" from the `JSONField` of the old Postgres
 contrib package (afaik no actual migration happened)
 * in my table, the corresponding field was correctly set to `jsonb`
 * downgrading to Django 3.1 makes it work

 I've resorted to using raw SQL for this query, as suggested by @Andrew.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31973#comment:19>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.17c37cf20fc30c0405a7d45bc4e764f8%40djangoproject.com.

Reply via email to