#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 BertrandBordage):
Here’s a ready-to-use workaround for `get_or_create`, as described
[https://www.reddit.com/r/django/comments/38j43l/django_orm_postgresql_and_array_field_equality/crw7y4i
in this redit post].
{{{#!python
def bugfree_get_or_create(manager, **kwargs):
"""
This is workaround for https://code.djangoproject.com/ticket/25091
"""
model = manager.model
defaults = kwargs.pop('defaults', {})
for k, array in kwargs.items():
if isinstance(model._meta.get_field(k), ArrayField):
for i, item in enumerate(array):
kwargs[k + '__%d' % i] = item
kwargs[k + '__len'] = len(array)
del kwargs[k]
try:
return manager.get(**kwargs)
except model.DoesNotExist:
kwargs.update(defaults)
return manager.create(**kwargs)
}}}
Instead of writing
`YourModel.objects.get_or_create(your_array_field=['abc', 'def'],
other_kwargs=…)`, write `bugfree_get_or_create(YourModel.objects,
your_array_field=['abc', 'def'], other_kwargs=…)`.
--
Ticket URL: <https://code.djangoproject.com/ticket/25091#comment:2>
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.fe583f2005ec57827250d0559a9fef7f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.