[Django] #32392: Support Cast() for ExclusionConstraint expression

21 views
Skip to first unread message

Django

unread,
Jan 28, 2021, 7:08:12 AM1/28/21
to django-...@googlegroups.com
#32392: Support Cast() for ExclusionConstraint expression
--------------------------------------------+------------------------
Reporter: Tilman Koschnick | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
I am using Cast() in an expression to be able to combine a bool field with
a range field:

{{{
from django.db import models
from django.contrib.postgres.constraints import ExclusionConstraint
from django.contrib.postgres.fields import DateTimeRangeField,
RangeOperators


class Demo(models.Model):

text_f = models.TextField()
bool_f = models.BooleanField()
range_f = DateTimeRangeField()

class Meta:
constraints = (
ExclusionConstraint(name='exclusion_constraint', expressions=(
('text_f', RangeOperators.EQUAL),
(models.functions.Cast('bool_f', models.IntegerField()),
RangeOperators.EQUAL),
('range_f', RangeOperators.OVERLAPS),
)),
)
}}}

Running the migration, I get the following error:

{{{
psycopg2.errors.SyntaxError: syntax error at or near "::"
LINE 1: ...t" EXCLUDE USING GIST ("text_f" WITH =, ("bool_f")::integer ...
^
}}}

Simply wrapping the left hand side of the expression in brackets solves
the problem, without ill effects as far as I can tell, see attached patch.

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

Django

unread,
Jan 28, 2021, 7:08:28 AM1/28/21
to django-...@googlegroups.com
#32392: Support Cast() for ExclusionConstraint expression
----------------------------------+--------------------------------------

Reporter: Tilman Koschnick | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Tilman Koschnick):

* Attachment "constraints.diff" added.

Django

unread,
Jan 28, 2021, 7:39:13 AM1/28/21
to django-...@googlegroups.com
#32392: ExclusionConstraint() crashes with Cast().
----------------------------------+------------------------------------

Reporter: Tilman Koschnick | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 3.1
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 Mariusz Felisiak):

* cc: Mads Jensen (added)
* has_patch: 1 => 0
* stage: Unreviewed => Accepted


Comment:

Thanks for the report. Please send PR with tests via GitHub.

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

Django

unread,
Jan 28, 2021, 12:03:10 PM1/28/21
to django-...@googlegroups.com
#32392: ExclusionConstraint() crashes with Cast().
-------------------------------------+-------------------------------------
Reporter: Tilman Koschnick | Owner: Tilman
| Koschnick
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 3.1

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 Tilman Koschnick):

* owner: (none) => Tilman Koschnick
* status: new => assigned
* has_patch: 0 => 1


Comment:

I had to move adding of parentheses into the Cast() class to make all
tests pass. Tests run under PostgreSQL.
[https://github.com/django/django/pull/13942 PR]

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

Django

unread,
Jan 29, 2021, 4:44:24 AM1/29/21
to django-...@googlegroups.com
#32392: ExclusionConstraint() crashes with Cast().
-------------------------------------+-------------------------------------
Reporter: Tilman Koschnick | Owner: Tilman
| Koschnick
Type: Bug | Status: assigned
Component: contrib.postgres | Version: 3.1
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/32392#comment:3>

Django

unread,
Jan 29, 2021, 5:26:53 AM1/29/21
to django-...@googlegroups.com
#32392: ExclusionConstraint() crashes with Cast().
-------------------------------------+-------------------------------------
Reporter: Tilman Koschnick | Owner: Tilman
| Koschnick
Type: Bug | Status: closed
Component: contrib.postgres | Version: 3.1
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:"fdfbc66331292def201c9344e3cd29fbcbcd076a" fdfbc663]:
{{{
#!CommitTicketReference repository=""
revision="fdfbc66331292def201c9344e3cd29fbcbcd076a"
Fixed #32392 -- Fixed ExclusionConstraint crash with Cast() in
expressions.
}}}

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

Django

unread,
Jan 29, 2021, 5:27:18 AM1/29/21
to django-...@googlegroups.com
#32392: ExclusionConstraint() crashes with Cast().
-------------------------------------+-------------------------------------
Reporter: Tilman Koschnick | Owner: Tilman
| Koschnick
Type: Bug | Status: closed
Component: contrib.postgres | Version: 3.1

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:"18cac6bbfb3cdc02d4891ad01d9f358cce96e86a" 18cac6b]:
{{{
#!CommitTicketReference repository=""
revision="18cac6bbfb3cdc02d4891ad01d9f358cce96e86a"
[3.2.x] Fixed #32392 -- Fixed ExclusionConstraint crash with Cast() in
expressions.

Backport of fdfbc66331292def201c9344e3cd29fbcbcd076a from master
}}}

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

Django

unread,
Jun 22, 2021, 1:09:20 AM6/22/21
to django-...@googlegroups.com
#32392: ExclusionConstraint() crashes with Cast().
-------------------------------------+-------------------------------------
Reporter: Tilman Koschnick | Owner: Tilman
| Koschnick
Type: Bug | Status: closed
Component: contrib.postgres | Version: 3.1

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/32392#comment:6>

Reply all
Reply to author
Forward
0 new messages