﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
32392	ExclusionConstraint() crashes with Cast().	Tilman Koschnick	Tilman Koschnick	"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."	Bug	closed	contrib.postgres	3.1	Normal	fixed		Mads Jensen	Ready for checkin	1	0	0	0	0	0
