#25091: Array field equality lookup fails with ProgrammingError
----------------------------------+------------------------------------
     Reporter:  unklphil          |                    Owner:
         Type:  Bug               |                   Status:  new
    Component:  contrib.postgres  |                  Version:  master
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Accepted
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+------------------------------------

Comment (by calvingiles):

 I have found two simple workarounds to this, but they may have some issues
 that I am not aware of. The first is to pass the array to the get or
 get_or_create method not as a list, but as a string formatted using

 {{{
   '{1.0, 2.0}'
 }}}

 instead of

 {{{
   ARRAY[1.0, 2.0]
 }}}

 which causes postgres to handle an implicit cast from numeric to real
 (which I needed in this case).

 The other is to perform this conversion in `get_db_prep_value()` by
 overriding the ArrayField class. For my specific case, this appears to
 work, but it may not be robust to arrays of different types (especially
 strings):

 {{{
 class StrFormatArrayField(ArrayField):
     """
     Override the array format to use "'{1.0, 2.0, 3.0}'" instead of
 "ARRAY([1.0, 2.0, 3.0])"
       to get around an issue with implicit type conversion.
     """
     def get_db_prep_value(self, value, connection, prepared=False):
         if isinstance(value, list) or isinstance(value, tuple):
             prepped = [self.base_field.get_db_prep_value(i, connection,
 prepared) for i in value]
             joined = ', '.join([str(v) for v in prepped])
             return '{' + joined + '}'
         else:
             return value
 }}}


 Any advice on improving upon this or issues I may face would be greatly
 appreciated.

--
Ticket URL: <https://code.djangoproject.com/ticket/25091#comment:4>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.5c92820214e5075d0fbafa875fb9efcd%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to