Opened 8 months ago

Closed 8 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!

Change History (1)

comment:1 by Mariusz Felisiak, 8 months ago

Resolution: invalid
Status: newclosed

You need to add django.contrib.postgres to the INSTALLED_APPS this will register PostgreSQL-specific serializers.

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