Opened 7 years ago

Closed 7 years ago

Last modified 5 years 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 by Simon Charette, 7 years ago

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 by Simon Charette, 7 years ago

Owner: set to Simon Charette
Status: newassigned

comment:3 by Simon Charette, 7 years ago

Has patch: set

comment:4 by Tim Graham, 7 years ago

Triage Stage: AcceptedReady for checkin

in reply to:  3 comment:5 by Hendrik Richter, 7 years ago

Replying to Simon Charette:

PR

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

comment:6 by Simon Charette <charette.s@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In b9186850:

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

Thanks Tim for the review.

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

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 by Tim Graham <timograham@…>, 5 years ago

In a8b03bea:

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

comment:9 by Tim Graham <timograham@…>, 5 years ago

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