[Django] #32858: Syntax error when using an index transform on an ArrayField in an ExclusionConstraint

9 views
Skip to first unread message

Django

unread,
Jun 17, 2021, 7:03:14 AM6/17/21
to django-...@googlegroups.com
#32858: Syntax error when using an index transform on an ArrayField in an
ExclusionConstraint
-------------------------------------+-------------------------------------
Reporter: Lucidiot | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I am trying to create a GIST exclusion constraint on a PostgreSQL 12
database. One of the fields is an ArrayField, and I want to apply the
constraint on its first item using `my_array__0`. When doing so, I get a
syntax error from Postgres.

{{{#!python
from django.db import models
from django.contrib.postgres.constraints import ExclusionConstraint
from django.contrib.postgres.fields import ArrayField, RangeOperators


class MyModel(models.Model):

my_array = models.ArrayField()

class Meta:
constraints = (
ExclusionConstraint(
name='foo',
expressions=[
('my_array__0', RangeOperators.EQUAL),
]
)
)
}}}

{{{
django.db.utils.ProgrammingError: syntax error at or near "WITH"
LINE 1: ..." ADD CONSTRAINT "foo" EXCLUDE USING GIST ("my_array"[1] WITH
=)
^
}}}

It seems a similar issue had occurred with casts (#32392). The
[https://www.postgresql.org/docs/current/sql-altertable.html docs for
ALTER TABLE] mention this syntax for expressions in an EXCLUDE constraint:

{{{
exclude_element in an EXCLUDE constraint is:

{ column_name | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS {
FIRST | LAST } ]
}}}

This implies that parentheses around anything that is not a column name
are mandatory. Maybe a more generic fix could be made to detect if
something is only a column name, or just to add parentheses all the time?
`EXCLUDE USING GIST (("my_array"[1]) WITH =)` works without any trouble.

--
Ticket URL: <https://code.djangoproject.com/ticket/32858>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 17, 2021, 7:04:03 AM6/17/21
to django-...@googlegroups.com
#32858: Syntax error when using an index transform on an ArrayField in an
ExclusionConstraint
-------------------------------------+-------------------------------------
Reporter: Lucidiot | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Lucidiot:

Old description:

New description:

I am trying to create a GIST exclusion constraint on a PostgreSQL 12
database. One of the fields is an ArrayField, and I want to apply the
constraint on its first item using `my_array__0`. When doing so, I get a
syntax error from Postgres.

{{{#!python
from django.db import models
from django.contrib.postgres.constraints import ExclusionConstraint
from django.contrib.postgres.fields import ArrayField, RangeOperators


class MyModel(models.Model):

my_array = ArrayField()

class Meta:
constraints = (
ExclusionConstraint(
name='foo',
expressions=[
('my_array__0', RangeOperators.EQUAL),
]
)
)
}}}

{{{
django.db.utils.ProgrammingError: syntax error at or near "WITH"
LINE 1: ..." ADD CONSTRAINT "foo" EXCLUDE USING GIST ("my_array"[1] WITH
=)
^
}}}

It seems a similar issue had occurred with casts (#32392). The
[https://www.postgresql.org/docs/current/sql-altertable.html docs for
ALTER TABLE] mention this syntax for expressions in an EXCLUDE constraint:

{{{
exclude_element in an EXCLUDE constraint is:

{ column_name | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS {
FIRST | LAST } ]
}}}

This implies that parentheses around anything that is not a column name
are mandatory. Maybe a more generic fix could be made to detect if
something is only a column name, or just to add parentheses all the time?
`EXCLUDE USING GIST (("my_array"[1]) WITH =)` works without any trouble.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32858#comment:1>

Django

unread,
Jun 17, 2021, 10:23:53 AM6/17/21
to django-...@googlegroups.com
#32858: Syntax error when using an index transform on an ArrayField in an
ExclusionConstraint
-------------------------------------+-------------------------------------
Reporter: Lucidiot | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted


Comment:

The proposed solution makes sense to me, looks like #32392 was just a
workaround in the end.

It should be easy enough to add the conditional `f'({sql})'` wrapping if
`isinstance(expression, Col)` as you've mentioned (`Col` is
`django.db.models.expressions.Col`).

https://github.com/django/django/blob/225d96533a8e05debd402a2bfe566487cc27d95f/django/contrib/postgres/constraints.py#L72-L83

Would you be able to provide a patch/PR?

I also wonder if we should just revert the non-test bits of
fdfbc66331292def201c9344e3cd29fbcbcd076a since it might just be hiding
other instances of this problem.

--
Ticket URL: <https://code.djangoproject.com/ticket/32858#comment:2>

Django

unread,
Jun 17, 2021, 4:34:43 PM6/17/21
to django-...@googlegroups.com
#32858: Syntax error when using an index transform on an ArrayField in an
ExclusionConstraint
-------------------------------------+-------------------------------------
Reporter: Lucidiot | Owner: Lucidiot
Type: Bug | Status: assigned

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Lucidiot):

* owner: nobody => Lucidiot
* status: new => assigned
* has_patch: 0 => 1


Comment:

I managed to make a [https://github.com/django/django/pull/14536 PR]! I
did not however look into reverting the previous fix.

--
Ticket URL: <https://code.djangoproject.com/ticket/32858#comment:3>

Django

unread,
Jun 17, 2021, 4:54:10 PM6/17/21
to django-...@googlegroups.com
#32858: Syntax error when using an index transform on an ArrayField in an
ExclusionConstraint
-------------------------------------+-------------------------------------
Reporter: Lucidiot | Owner: Lucidiot
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

That's looking great thanks! Lets leave the decision of reverting the
previous fix to the mergers.

--
Ticket URL: <https://code.djangoproject.com/ticket/32858#comment:4>

Django

unread,
Jun 22, 2021, 12:23:11 AM6/22/21
to django-...@googlegroups.com
#32858: Syntax error when using an index transform on an ArrayField in an
ExclusionConstraint
-------------------------------------+-------------------------------------
Reporter: Lucidiot | Owner: Lucidiot
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32858#comment:5>

Django

unread,
Jun 22, 2021, 1:09:20 AM6/22/21
to django-...@googlegroups.com
#32858: Syntax error when using an index transform on an ArrayField in an
ExclusionConstraint
-------------------------------------+-------------------------------------
Reporter: Lucidiot | Owner: Lucidiot
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"e07609a0d1cac573890380ee32b85d7743632650" e07609a0]:
{{{
#!CommitTicketReference repository=""
revision="e07609a0d1cac573890380ee32b85d7743632650"
Refs #32858, Refs #32392 -- Restored using :: shortcut syntax in Cast() on
PostgreSQL.

This partly reverts commit fdfbc66331292def201c9344e3cd29fbcbcd076a
unnecessary since b69b0c3fe871167a0ca01bb439508e335143801f.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32858#comment:7>

Django

unread,
Jun 22, 2021, 1:09:20 AM6/22/21
to django-...@googlegroups.com
#32858: Syntax error when using an index transform on an ArrayField in an
ExclusionConstraint
-------------------------------------+-------------------------------------
Reporter: Lucidiot | Owner: Lucidiot
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"b69b0c3fe871167a0ca01bb439508e335143801f" b69b0c3f]:
{{{
#!CommitTicketReference repository=""
revision="b69b0c3fe871167a0ca01bb439508e335143801f"
Fixed #32858 -- Fixed ExclusionConstraint crash with index transforms in
expressions.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32858#comment:6>

Reply all
Reply to author
Forward
0 new messages