#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:

> 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.

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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.6f8da74018b0c60edfe65e0c0f9a1391%40djangoproject.com.

Reply via email to