Opened 19 months ago
Closed 19 months ago
#35431 closed Bug (invalid)
Failing to serialize NumericRange with CheckConstraint on IntegerRangeField
| Reported by: | Arran | Owned by: | nobody |
|---|---|---|---|
| Component: | Migrations | Version: | 5.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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!
Note:
See TracTickets
for help on using tickets.
You need to add
django.contrib.postgresto theINSTALLED_APPSthis will register PostgreSQL-specific serializers.