[Django] #30350: CheckConstraint repeatedly deleted and re-added

16 views
Skip to first unread message

Django

unread,
Apr 11, 2019, 11:23:06 AM4/11/19
to django-...@googlegroups.com
#30350: CheckConstraint repeatedly deleted and re-added
-------------------------------------+-------------------------------------
Reporter: Sigurd | Owner: nobody
Ljødal |
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
A `CheckConstraint` with a `Q(x__in=range(y, z))` condition is repeatedly
deleted and re-added when running `makemigrations`.


{{{#!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.

Django

unread,
Apr 12, 2019, 2:07:21 AM4/12/19
to django-...@googlegroups.com
#30350: CheckConstraint repeatedly deleted and re-added
-------------------------------------+-------------------------------------
Reporter: Sigurd Ljødal | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Apr 12, 2019, 4:14:40 AM4/12/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains iterator.
---------------------------------+------------------------------------

Reporter: Sigurd Ljødal | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by felixxm):

* 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>

Django

unread,
Apr 12, 2019, 4:55:47 AM4/12/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains iterator.
---------------------------------+------------------------------------
Reporter: Sigurd Ljødal | Owner: felixxm
Type: Bug | Status: assigned

Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by felixxm):

* owner: nobody => felixxm
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:3>

Django

unread,
Apr 12, 2019, 5:33:24 AM4/12/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains iterator.
---------------------------------+------------------------------------
Reporter: Sigurd Ljødal | Owner: felixxm
Type: Bug | Status: assigned
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

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>

Django

unread,
Apr 13, 2019, 10:21:46 AM4/13/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains iterator.
-------------------------------------+-------------------------------------
Reporter: Sigurd Ljødal | Owner: Florian
| Apolloner

Type: Bug | Status: assigned
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* owner: felixxm => Florian Apolloner
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:5>

Django

unread,
Apr 14, 2019, 6:54:23 AM4/14/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains iterator.
-------------------------------------+-------------------------------------
Reporter: Sigurd Ljødal | Owner: Florian
| Apolloner
Type: Bug | Status: closed
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Django

unread,
Apr 14, 2019, 7:01:15 AM4/14/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains iterator.
-------------------------------------+-------------------------------------
Reporter: Sigurd Ljødal | Owner: Florian
| Apolloner
Type: Bug | Status: closed
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Apr 14, 2019, 7:05:35 PM4/14/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains a range object.

-------------------------------------+-------------------------------------
Reporter: Sigurd Ljødal | Owner: Florian
| Apolloner
Type: Bug | Status: closed
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/30350#comment:8>

Django

unread,
Sep 24, 2019, 7:55:35 AM9/24/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains a range object.
-------------------------------------+-------------------------------------
Reporter: Sigurd Ljødal | Owner: Florian
| Apolloner
Type: Bug | Status: closed
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 24, 2019, 7:56:19 AM9/24/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains a range object.
-------------------------------------+-------------------------------------
Reporter: Sigurd Ljødal | Owner: Florian
| Apolloner
Type: Bug | Status: closed
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 24, 2019, 7:57:17 AM9/24/19
to django-...@googlegroups.com
#30350: Migration re-add check constraint continuously when check condition
contains a range object.
-------------------------------------+-------------------------------------
Reporter: Sigurd Ljødal | Owner: Florian
| Apolloner
Type: Bug | Status: closed
Component: Migrations | Version: 2.2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages