#30715 closed Bug (fixed)
ArrayField lookups crash on ArrayAgg() over AutoFields.
Reported by: | Mikail | Owned by: | Mariusz Felisiak |
---|---|---|---|
Component: | contrib.postgres | Version: | dev |
Severity: | Normal | Keywords: | array, serial, postgres, psql, auto, id |
Cc: | Patryk Zawadzki | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When doing a annotation using the ArrayAgg()
function over the id/pk
(django.db.models.fields.AutoField
) field it is casted to serial[]
which is an invalid type in pSQL.
From this, we get the following error (caused by https://github.com/django/django/blob/b10d322c41f66dc7c77c36f90a3532269b25ea93/django/contrib/postgres/fields/array.py#L81-L83):
psycopg2.errors.UndefinedObject: type "serial[]" does not exist LINE 1: ... HAVING ARRAY_AGG("item_item"."id" ) @> ARRAY[1,2]::serial[]
Example:
from django.db import models class Item(models.Model): pass
from django.contrib.postgres.aggregates import ArrayAgg from django.db.models import F from django.test import TestCase from .models import Item class TestQueryAutoIDAsArray(TestCase): def test_query(self): qs = Item.objects.annotate(pk_list=ArrayAgg(F("id"))) qs = qs.filter(pk_list__contains=[1, 2]) assert len(qs[:]) == 0
Maybe we should cast auto fields to int[]
?
Change History (13)
comment:1 by , 5 years ago
Cc: | added |
---|
comment:2 by , 5 years ago
Summary: | ArrayField is converting AutoField to serial[] being an invalid pSQL type → ArrayField lookups crash on ArrayAgg() over AutoFields. |
---|---|
Triage Stage: | Unreviewed → Accepted |
Version: | 2.2 → master |
comment:3 by , 5 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:4 by , 5 years ago
Hi Adam I solved this when preparing PR 11699. Are you OK to reassign ticket to me?
comment:7 by , 5 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:9 by , 5 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
Version: | master → 2.2 |
Hi felixxm, sorry to tell, but this issue still present in Django 2.2.12 and 2.2.x branch as well.
Also it does not affect only pk
fields, but at least ForeignKey
too.
Could you please apply your fix to 2.2?
comment:10 by , 5 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Version: | 2.2 → master |
The issue has been present since the feature was introduced. Per our backporting policy this means it doesn't qualify for a backport to 2.2.x anymore.
See https://docs.djangoproject.com/en/dev/internals/release-process/ for more details.
follow-up: 13 comment:12 by , 4 years ago
rizzaro, you can apply the patch to your local version or your own fork.
comment:13 by , 4 years ago
Unfortunately this is not possible on my situation but I've found JSONBAgg
works for my case. Thanks!
Thanks for the report, all
ArrayField
's lookups are affected, using aCast()
expression (that already handle this) instead of'%s::%s'
should fix this issue (tests should land inpostgres_tests/test_array.py
).