Opened 2 years ago

Closed 2 years ago

Last modified 7 months ago

#28161 closed Bug (fixed)

contrib.postgres: ArrayField(CITextField) returns string instead of list

Reported by: Hendrik Richter Owned by: Simon Charette
Component: contrib.postgres Version: 1.11
Severity: Release blocker Keywords:
Cc: 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

The following code

from django.db import models
from django.contrib.postgres.fields import ArrayField, CITextField

class Foo(models.Model):
    bar = ArrayField(models.TextField)
    baz = ArrayField(CITextField())

has this wrong behaviour:

x = Foo.objects.get(id=1)
x.bar #  => ["Foo", "Bar"]
x.baz #  => "{Foo,Bar}"

This is due to https://github.com/psycopg/psycopg2/issues/268 which requires registering a new adapter for citext fields:

Use

select typarray from pg_type where typname = 'citext';

to get the oid, then do

psycopg2.extensions.register_type(
    psycopg2.extensions.new_array_type(
        (the_returned_oid,), 'citext[]', psycopg2.STRING))

for the correct behaviour in plain psycopg2.

Django should do that automatically if any of the CI*Fields is used. Alternatively, the ArrayField should convert the result string manually to the proper list.

Change History (9)

comment:1 Changed 2 years ago by Simon Charette

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

We'll have to do the same thing we do for hstore.

Marking as release blocker because it's a bug in a newly introduced feature.

comment:2 Changed 2 years ago by Simon Charette

Owner: set to Simon Charette
Status: newassigned

comment:3 Changed 2 years ago by Simon Charette

Has patch: set

comment:4 Changed 2 years ago by Tim Graham

Triage Stage: AcceptedReady for checkin

comment:5 in reply to:  3 Changed 2 years ago by Hendrik Richter

Replying to Simon Charette:

PR

Thank you for the quick fix, that helps a lot :-) Awesome!

comment:6 Changed 2 years ago by Simon Charette <charette.s@…>

Resolution: fixed
Status: assignedclosed

In b9186850:

Fixed #28161 -- Fixed return type of ArrayField(CITextField()).

Thanks Tim for the review.

comment:7 Changed 2 years ago by Simon Charette <charette.s@…>

In 246166cf:

[1.11.x] Fixed #28161 -- Fixed return type of ArrayField(CITextField()).

Thanks Tim for the review.

Backport of b91868507af08234a30e9a8e7c90b37c561ba315 from master.

comment:8 Changed 7 months ago by Tim Graham <timograham@…>

In a8b03bea:

Refs #28161 -- Doc'd INSTALLED_APPS requirement for ArrayField(CIText).

comment:9 Changed 7 months ago by Tim Graham <timograham@…>

In 953067d8:

[2.2.x] Refs #28161 -- Doc'd INSTALLED_APPS requirement for ArrayField(CIText).

Backport of a8b03bea180e0660c0e159f3e7cf6192b512925f from master

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