{{{#!python
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:
{{{#!python
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:
1. Keep the `range` method call in the generated migration file
2. Disallow using ranges in check constraints
3. (At least on PostgreSQL, we could use a range expression in the
database too.)
--
Ticket URL: <https://code.djangoproject.com/ticket/30350>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by felixxm):
In check constraints you can use `range` lookup and it works fine, e.g.
{{{#!python
models.CheckConstraint(
check=models.Q(month__range=[1, 13]),
name='check_valid_month',
)
}}}
also casting an iterator to a list works good, e.g.
{{{#!python
models.CheckConstraint(
check=models.Q(month__in=list(range(1, 13))),
name='check_valid_month',
)
}}}
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:1>
* cc: Ian Foote (added)
* component: Database layer (models, ORM) => Migrations
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:2>
* owner: nobody => felixxm
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:3>
Comment (by Sigurd Ljødal):
Just to confirm, I modified the migration to use `range()` and
`makemigrations` no longer detects any changes.
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:4>
* owner: felixxm => Florian Apolloner
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:5>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"2e38f2015aba224b68a91a3012b87223f3046bb6" 2e38f20]:
{{{
#!CommitTicketReference repository=""
revision="2e38f2015aba224b68a91a3012b87223f3046bb6"
Fixed #30350 -- Prevented recreation of migration for operations with a
range object.
Thanks to Mariusz Felisiak for helping with the patch.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:6>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"896cc7190115c5bcdef756fe10dc203d9b03678c" 896cc719]:
{{{
#!CommitTicketReference repository=""
revision="896cc7190115c5bcdef756fe10dc203d9b03678c"
[2.2.x] Fixed #30350 -- Prevented recreation of migration for operations
with a range object.
Thanks to Mariusz Felisiak for helping with the patch.
Backport of 2e38f2015aba224b68a91a3012b87223f3046bb6 from master.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:7>
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:8>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"bc46e386c7aa496642d3ffc9e4f56ae5a46417a7" bc46e386]:
{{{
#!CommitTicketReference repository=""
revision="bc46e386c7aa496642d3ffc9e4f56ae5a46417a7"
Refs #30350 -- Doc'd support for range serialization in migrations.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:9>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"0098f26fdd0a56d6613585de9eec2f34499c3287" 0098f26f]:
{{{
#!CommitTicketReference repository=""
revision="0098f26fdd0a56d6613585de9eec2f34499c3287"
[3.0.x] Refs #30350 -- Doc'd support for range serialization in
migrations.
Backport of bc46e386c7aa496642d3ffc9e4f56ae5a46417a7 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:10>
Comment (by Mariusz Felisiak <felisiak.mariusz@…>):
In [changeset:"acc0d99e6c1388199ca2275b179051e5a89cdc25" acc0d99e]:
{{{
#!CommitTicketReference repository=""
revision="acc0d99e6c1388199ca2275b179051e5a89cdc25"
[2.2.x] Refs #30350 -- Doc'd support for range serialization in
migrations.
Backport of bc46e386c7aa496642d3ffc9e4f56ae5a46417a7 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:11>