[Django] #35431: Failing to serialize NumericRange with CheckConstraint on IntegerRangeField

7 views
Skip to first unread message

Django

unread,
May 4, 2024, 8:49:12 AMMay 4
to django-...@googlegroups.com
#35431: Failing to serialize NumericRange with CheckConstraint on IntegerRangeField
--------------------------------------+------------------------
Reporter: Arran | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
I am trying to create a PostgreSQL check constraint on an
`IntegerRangeField`, but when I run `makemigrations` I get the following
error:

{{{
ValueError: Cannot serialize: Range(1, 10000, '[)')
There are some values Django cannot serialize into migration files.
}}}

Here's a simple example of my model which raises the error:

{{{
from django.contrib.postgres.fields import IntegerRangeField
from django.db.backends.postgresql.psycopg_any import NumericRange
from django.db import models
from django.db.models.constraints import CheckConstraint
from django.db.models.expressions import Q


class MyModel(models.Model):
price_range = IntegerRangeField()

class Meta:
constraints = [
CheckConstraint(
check=Q(price_range__contained_by=NumericRange(1, 10000)),
name="price_range_gt_1_and_lte_10000",
)
]
}}}

Judging by the following ticket this should work, so any help would be
welcome:

[https://code.djangoproject.com/ticket/30258]

Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/35431>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 5, 2024, 10:37:39 AMMay 5
to django-...@googlegroups.com
#35431: Failing to serialize NumericRange with CheckConstraint on IntegerRangeField
----------------------------+--------------------------------------
Reporter: Arran | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* resolution: => invalid
* status: new => closed

Comment:

You need to add `django.contrib.postgres` to the `INSTALLED_APPS` this
will register PostgreSQL-specific serializers.
--
Ticket URL: <https://code.djangoproject.com/ticket/35431#comment:1>
Reply all
Reply to author
Forward
0 new messages