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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.0df00dadc8f4aae6140b41d2dd57e884%40djangoproject.com.

Reply via email to