Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#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 Patryk Zawadzki, 5 years ago

Cc: Patryk Zawadzki added

comment:2 by Mariusz Felisiak, 5 years ago

Summary: ArrayField is converting AutoField to serial[] being an invalid pSQL typeArrayField lookups crash on ArrayAgg() over AutoFields.
Triage Stage: UnreviewedAccepted
Version: 2.2master

Thanks for the report, all ArrayField's lookups are affected, using a Cast() expression (that already handle this) instead of '%s::%s' should fix this issue (tests should land in postgres_tests/test_array.py).

comment:3 by Adnan Umer, 5 years ago

Owner: set to Adnan Umer
Status: newassigned

comment:4 by Mariusz Felisiak, 5 years ago

Hi Adam I solved this when preparing PR 11699. Are you OK to reassign ticket to me?

comment:5 by Mariusz Felisiak, 5 years ago

Has patch: set

comment:6 by Adnan Umer, 5 years ago

Owner: changed from Adnan Umer to Mariusz Felisiak

No problem felixxm

comment:7 by Carlton Gibson, 5 years ago

Triage Stage: AcceptedReady for checkin

comment:8 by Carlton Gibson <carlton@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 521308e5:

Fixed #30715 -- Fixed crash of ArrayField lookups on ArrayAgg annotations over AutoField.

comment:9 by Ivan Piskunov, 4 years ago

Resolution: fixed
Status: closednew
Version: master2.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 Mariusz Felisiak, 4 years ago

Resolution: fixed
Status: newclosed
Version: 2.2master

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.

comment:11 by rizzaro, 4 years ago

Is there a workaround for older versions, felixxm? Thank you

comment:12 by Mariusz Felisiak, 4 years ago

rizzaro, you can apply the patch to your local version or your own fork.

in reply to:  12 comment:13 by rizzaro, 4 years ago

Unfortunately this is not possible on my situation but I've found JSONBAgg works for my case. Thanks!

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