{{{
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.
* Attachment "constraints.diff" added.
* 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>
* 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>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32392#comment:3>
* 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>
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>
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>