Opened 7 years ago

Closed 4 years ago

Last modified 4 years ago

#27808 closed Bug (fixed)

Nested ArrayField with nullable base field generates invalid SQL

Reported by: Josef Kolář Owned by: Hasan Ramezani
Component: contrib.postgres Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Josef Kolář)

When I have model

class NestedNullableIntegerArrayModel(PostgreSQLModel):
    field = ArrayField(ArrayField(models.IntegerField(null=True)))

and trying to save data

NestedNullableIntegerArrayModel(field=[[None, None], [None, None]]).save()

Django generates

INSERT INTO "postgres_tests_nestednullableintegerarraymodel" ("field") VALUES (%s) RETURNING "postgres_tests_nestednullableintegerarraymodel"."id"

with expectable params

([[None, None], [None, None]], )

but this query fails in postgres on

ERROR:  column "field" is of type integer[] but expression is of type text[]
LINE 1: ...estednullableintegerarraymodel" ("field") VALUES (ARRAY['{NU...
                                                             ^
HINT:  You will need to rewrite or cast the expression.

But, if I use one of values is not None, model is saved successfully:

NestedNullableIntegerArrayModel(field=[[None, None], [None, 42]]).save()

Is it a problem of Django or I should find problem in psycopg?

Change History (10)

comment:1 by Josef Kolář, 7 years ago

Description: modified (diff)

comment:2 by Josef Kolář, 7 years ago

Description: modified (diff)

comment:3 by Josef Kolář, 7 years ago

Description: modified (diff)

comment:4 by Simon Charette, 7 years ago

Version: 1.9master

Not sure if it's a duplicate but it looks related to #24726 at least.

comment:5 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:6 by Marcus Gregersen, 7 years ago

This is due to following bug in psycopg2 https://github.com/psycopg/psycopg2/issues/325

comment:7 by Hasan Ramezani, 4 years ago

Has patch: set
Owner: set to Hasan Ramezani
Status: newassigned

Seems bug was fixed in psycopg2. I just added a test case to prove it.

comment:8 by Mariusz Felisiak, 4 years ago

Bug is fixed in psycopg2 2.7.5+.

comment:9 by Mariusz Felisiak, 4 years ago

Has patch: unset
Resolution: fixed
Status: assignedclosed

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 84633905:

Refs #27808 -- Added test for saving nested ArrayField with nullable base field.

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