Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

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

constraints.diff (638 bytes ) - added by Tilman Koschnick 4 years ago.

Download all attachments as: .zip

Change History (7)

by Tilman Koschnick, 4 years ago

Attachment: constraints.diff added

comment:1 by Mariusz Felisiak, 4 years ago

Cc: Mads Jensen added
Has patch: unset
Summary: Support Cast() for ExclusionConstraint expressionExclusionConstraint() crashes with Cast().
Triage Stage: UnreviewedAccepted

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

comment:2 by Tilman Koschnick, 4 years ago

Has patch: set
Owner: set to Tilman Koschnick
Status: newassigned

I had to move adding of parentheses into the Cast() class to make all tests pass. Tests run under PostgreSQL.
PR

comment:3 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In fdfbc663:

Fixed #32392 -- Fixed ExclusionConstraint crash with Cast() in expressions.

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 18cac6b:

[3.2.x] Fixed #32392 -- Fixed ExclusionConstraint crash with Cast() in expressions.

Backport of fdfbc66331292def201c9344e3cd29fbcbcd076a from master

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In e07609a0:

Refs #32858, Refs #32392 -- Restored using :: shortcut syntax in Cast() on PostgreSQL.

This partly reverts commit fdfbc66331292def201c9344e3cd29fbcbcd076a
unnecessary since b69b0c3fe871167a0ca01bb439508e335143801f.

Note: See TracTickets for help on using tickets.
Back to Top