#31991: .raw method returns string instead of dict
-------------------------------------+-------------------------------------
               Reporter:  horpto     |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.1
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  JSONfield
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Imagine 2 tables:

 {{{
 class AtsPhoneCall(models.Model):
     created = models.DateTimeField(auto_now_add=True, db_index=True)

     class Meta:
         db_table = 'atscall'

 class PhoneCall(models.Model):
     ats_call = models.OneToOneField(
         'phones.AtsPhoneCall',
         on_delete=models.PROTECT,
         related_name='call',
     )
     ivr_data = models.JSONField(null=True)
 }}}

 And raw query with join will give us wrong value of ivr_data. Old
 **django.contrib.postgres.fields.JSONField** returned dict value as new
 **django.db.models.JSONField** returns str instead of dict. Example from
 django shell:


 {{{
 >>> sql = '''
     join phones_phonecall as p on atscall.id = p.ats_call_id
     where ivr_data is not null
     limit 10
 '''
 ...     select atscall.id, p.ivr_data
 ...     from atscall
 ...     join phones_phonecall as p on atscall.id = p.ats_call_id
 ...     where ivr_data is not null
 ...     limit 10
 ... '''
 >>>
 >>> AtsPhoneCall.objects.raw(sql)[0].ivr_data
 DEBUG;(0.002)
     select atscall.id, p.ivr_data
     from atscall
     join phones_phonecall as p on atscall.id = p.ats_call_id
     where ivr_data is not null
     limit 10
 ; args=()
 '{"v1": []}'
 >>>
 >>> for i in AtsPhoneCall.objects.raw(sql):
 ...     print(i.id, i.ivr_data, type(i.ivr_data))
 ...     break
 ...
 DEBUG;(0.002)
     select atscall.id, p.ivr_data
     from atscall
     join phones_phonecall as p on atscall.id = p.ats_call_id
     where ivr_data is not null
     limit 10
 ; args=()
 180621245012669 {"v1": []} <class 'str'>
 >>> next(AtsPhoneCall.objects.raw(sql).iterator()).ivr_data
 DEBUG;(0.008)
     select atscall.id, p.ivr_data
     from atscall
     join phones_phonecall as p on atscall.id = p.ats_call_id
     where ivr_data is not null
     limit 10
 ; args=()
 '{"v1": []}'

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31991>
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/049.f4dc25b2bf586806c36130eddc58ef69%40djangoproject.com.

Reply via email to