#25091: Array field equality lookup fails with ProgrammingError
----------------------------------+-----------------
Reporter: unklphil | Owner:
Type: Bug | Status: new
Component: contrib.postgres | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+-----------------
Note: This bug report is mostly based on a reddit post by Glueon:
http://redd.it/38j43l
With the model:
{{{
class MyModel(models.Model):
emails = ArrayField(models.EmailField())
}}}
When trying to fetch a row with a list of emails:
{{{
MyModel.objects.filter(emails=['[email protected]'])
}}}
the following error occurs:
{{{
ProgrammingError: operator does not exist: character varying[] = text[]
HINT: No operator matches the given name and argument type(s). You might
need to add explicit type casts.
}}}
Because the resulting query is:
{{{
SELECT * FROM some_table WHERE emails = ARRAY['[email protected]'];
}}}
By default type of `ARRAY['[email protected]']` is `text []` while Django
stores it as a `varchar []`.
Glueon suggests that casting the array to `varchar []` using raw sql
solves the problem:
{{{
SELECT * FROM some_table WHERE emails = ARRAY['[email protected]']::varchar[];
}}}
I did post some other workarounds in the comments of the reddit post, but
there are some situations in which the workarounds are not possible, such
as when using `get_or_create` or `update_or_create`.
--
Ticket URL: <https://code.djangoproject.com/ticket/25091>
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/051.ac4a824f0f47557c80fffbd13529e994%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.