#30350 closed Bug (fixed)
Migration re-add check constraint continuously when check condition contains a range object.
| Reported by: | Sigurd Ljødal | Owned by: | Florian Apolloner |
|---|---|---|---|
| Component: | Migrations | Version: | 2.2 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Ian Foote | Triage Stage: | Ready for checkin |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
A CheckConstraint with a Q(x__in=range(y, z)) condition is repeatedly deleted and re-added when running makemigrations.
models.CheckConstraint( check=models.Q(month__in=range(1, 13)), name='check_valid_month', )
The generated migration looks like this, so I suspect that the issue is because the range is converted into a tuple:
operations = [ migrations.RemoveConstraint( model_name='monthlybudget', name='check_valid_month', ), migrations.AddConstraint( model_name='monthlybudget', constraint=models.CheckConstraint(check=models.Q(month__in=(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)), name='check_valid_month'), ), ]
A sample project with this issue can be found here:
https://github.com/ljodal/djangocon-eu-2019
I'm open to fixing this issue myself, but I would like to clarify what would be a correct fix to this issue. I see at least two possible solutions, maybe three:
- Keep the
rangemethod call in the generated migration file - Disallow using ranges in check constraints
- (At least on PostgreSQL, we could use a range expression in the database too.)
Change History (11)
comment:1 by , 7 years ago
comment:2 by , 7 years ago
| Cc: | added |
|---|---|
| Component: | Database layer (models, ORM) → Migrations |
| Severity: | Normal → Release blocker |
| Summary: | CheckConstraint repeatedly deleted and re-added → Migration re-add check constraint continuously when check condition contains iterator. |
| Triage Stage: | Unreviewed → Accepted |
comment:3 by , 7 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:4 by , 7 years ago
Just to confirm, I modified the migration to use range() and makemigrations no longer detects any changes.
comment:5 by , 7 years ago
| Owner: | changed from to |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
comment:8 by , 7 years ago
| Summary: | Migration re-add check constraint continuously when check condition contains iterator. → Migration re-add check constraint continuously when check condition contains a range object. |
|---|
In check constraints you can use
rangelookup and it works fine, e.g.also casting an iterator to a list works good, e.g.
so in this case iterator is an issue. I would check later if this can be easily fix and if not we should document this limitation.