Opened 5 years ago

Closed 5 years ago

#30716 closed Bug (duplicate)

ArrayContains does not call get_db_prep_value method for base field

Reported by: Andrey Torsunov Owned by:
Component: contrib.postgres Version: 2.2
Severity: Normal Keywords: array postgres
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Example:

import enum
from django.db import models
from django.contrib.postgres.fields.array import ArrayField


class EnumField(models.TextField):
    # get_db_prep_value calls this method
    def get_prep_value(val):
        return val.name


class SomeEnum(enum.Enum):
    A = enum.auto()
    B = enum.auto()


class SomeModel(models.Model):
    array = ArrayField(EnumField())


SomeModel.objects.filter(array__contains=[SomeEnum.A, SomeEnum.B]).all()

Will cause:

   File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
    cursor.execute(sql, params)
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/raven/contrib/django/client.py", line 127, in execute
    return real_execute(self, sql, params)
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/user/.local/share/virtualenvs/tko-ck5fDNTQ/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: can't adapt type 'SomeEnum'

It can be fixed via overriding get_db_prep_lookup in ArrayContains:

def get_db_prep_lookup(self, value, connection):
    base_field = self.lhs.output_field.base_field
    value = [base_field.get_db_prep_value(x, connection) for x in value]
    return super().get_db_prep_lookup(value, connection)

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #29391.

Note: See TracTickets for help on using tickets.
Back to Top