#29731: Type conversion should be possible with OuterRef
-------------------------------------+-------------------------------------
     Reporter:  tienne-B             |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  2.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  postgresql, sql,     |             Triage Stage:
  json, outerref                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * status:  new => closed
 * resolution:   => invalid


Comment:

 Hey tienne-B,

 Unfortunately it's not possible for Django, nor PostgreSQL, to
 automatically determine that `context__key` (`"context" -> 'key'`) is a
 string by the untyped and dynamic nature of the `jsonb` type. From
 Django's perspective `context__key` is a `JSONField` and from PostgreSQL
 it is `jsonb`.

 This is the case because it's not possible to determine whether `url_key`
 should be converted to `jsonb` or `"context" -> 'key'` to `text` so it is
 necessary to provide an explicit cast as PostgreSQL suggests. I'd say that
 here both Django and PostgreSQL refuse the temptation to guess.

 Unfortunately calling `str` on `OuterRef` won't work as the Django ORM
 doesn't implement this kind of ''high level magic'' like ORM such as
 Peewee provide. In these cases you'll have to rely on either a `Cast`
 [https://docs.djangoproject.com/en/2.1/ref/models/database-functions/#cast
 function] or `KeyTextTransform` annotations instead.

 For example using `Cast`.

 {{{#!python
 subquery = SentMessageRecord.objects.annotate(
     context_key=Cast('context__key', models.CharField()),
 ).filter(
     tournament=self.tournament,
     email=OuterRef('email'),
     context_key=OuterRef('url_key'),
     event=SentMessageRecord.EVENT_TYPE_URL,
 )
 }}}

 Given this isn't a defect with Django I'd say this would qualify as a
 usage question and would better be addressed through support channels.
 Without deterring you from reporting legitimate bugs it's always better to
 confirm whether or not an issue can be resolved on support channels before
 escalating to this tracker which is used to track defect and feature
 requests.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29731#comment:1>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.3c30c06c45475f49059a069fad835a4d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to