#32392 closed Bug (fixed)
ExclusionConstraint() crashes with Cast().
| Reported by: | Tilman Koschnick | Owned by: | Tilman Koschnick |
|---|---|---|---|
| Component: | contrib.postgres | Version: | 3.1 |
| Severity: | Normal | Keywords: | |
| Cc: | Mads Jensen | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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.
Attachments (1)
Change History (7)
by , 5 years ago
| Attachment: | constraints.diff added |
|---|
comment:1 by , 5 years ago
| Cc: | added |
|---|---|
| Has patch: | unset |
| Summary: | Support Cast() for ExclusionConstraint expression → ExclusionConstraint() crashes with Cast(). |
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 5 years ago
| Has patch: | set |
|---|---|
| Owner: | set to |
| Status: | new → assigned |
I had to move adding of parentheses into the Cast() class to make all tests pass. Tests run under PostgreSQL.
PR
comment:3 by , 5 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Note:
See TracTickets
for help on using tickets.
Thanks for the report. Please send PR with tests via GitHub.