[Django] #34710: Infinite migrations using models.Choices

13 views
Skip to first unread message

Django

unread,
Jul 13, 2023, 7:32:53 AM7/13/23
to django-...@googlegroups.com
#34710: Infinite migrations using models.Choices
-----------------------------------------+------------------------
Reporter: divtxt | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Summary:
- Infinite migrations generated when using enum which is subclass of
models.Choices
- Issue does NOT occur with models.TextChoices
- Workaround: use default=EnumClass.SOME_VALUE**.value**

----

Steps to reproduce:

- In a new app, define the following model:

{{{
class CoinTossResult(models.Model):
class CoinFace(models.Choices):
HEADS = "h"
TAILS = "t"

result = models.CharField(
max_length=1,
choices=CoinFace.choices,
default=CoinFace.HEADS,
)
}}}

- Run makemigrations multiple times. After the initial migration, an extra
migration is generated repeatedly:

{{{
(myapp) ~/work/myapp % ./manage.py makemigrations
Migrations for 'foo':
foo/migrations/0001_initial.py
- Create model CoinTossResult

(myapp) ~/work/myapp % ./manage.py makemigrations
Migrations for 'foo':
foo/migrations/0002_alter_cointossresult_result.py
- Alter field result on cointossresult

(myapp) ~/work/myapp % ./manage.py makemigrations
Migrations for 'foo':
foo/migrations/0003_alter_cointossresult_result.py
- Alter field result on cointossresult

(myapp) ~/work/myapp % ./manage.py makemigrations
Migrations for 'foo':
foo/migrations/0004_alter_cointossresult_result.py
- Alter field result on cointossresult

(myapp) ~/work/myapp % cat
foo/migrations/0002_alter_cointossresult_result.py
# Generated by Django 4.2.2 on 2023-07-13 11:00

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('foo', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='cointossresult',
name='result',
field=models.CharField(choices=[('h', 'Heads'), ('t',
'Tails')], default='h', max_length=1),
),
]

(myapp) ~/work/myapp % diff
foo/migrations/0002_alter_cointossresult_result.py
foo/migrations/0003_alter_cointossresult_result.py
9c9
< ('foo', '0001_initial'),
---
> ('foo', '0002_alter_cointossresult_result'),

(myapp) ~/work/myapp % diff
foo/migrations/0003_alter_cointossresult_result.py
foo/migrations/0004_alter_cointossresult_result.py
9c9
< ('foo', '0002_alter_cointossresult_result'),
---
> ('foo', '0003_alter_cointossresult_result'),
}}}

----

Same steps while using the workaround:

{{{
(myapp) ~/work/myapp % git diff
diff --git a/foo/models.py b/foo/models.py
index 50e0ad0..e553ad7 100644
--- a/foo/models.py
+++ b/foo/models.py
@@ -8,5 +8,5 @@ class CoinTossResult(models.Model):
result = models.CharField(
max_length=1,
choices=CoinFace.choices,
- default=CoinFace.HEADS,
+ default=CoinFace.HEADS.value,
)

(myapp) ~/work/myapp % ./manage.py makemigrations foo
Migrations for 'foo':
foo/migrations/0001_initial.py
- Create model CoinTossResult

(myapp) ~/work/myapp % ./manage.py makemigrations foo
No changes detected in app 'foo'
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34710>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 13, 2023, 7:57:51 AM7/13/23
to django-...@googlegroups.com
#34710: Infinite migrations using models.Choices
-------------------------------+--------------------------------------
Reporter: Div Shekhar | Owner: nobody

Type: Uncategorized | Status: new
Component: Migrations | Version: 4.2
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 David Sanders):

Hi Div Shekhar,

In order to get your example working you'd need to specify the type like
so:

{{{
class CoinTossResult(models.Model):
class CoinFace(str, models.Choices):
...
}}}

I don't think this is an issue with Django – though there may be some
documentation updates that could make this clearer when defining custom
`Choices` classes. I'll leave it up to felixx or nessita to decide whether
this ticket is invalid 😊

--
Ticket URL: <https://code.djangoproject.com/ticket/34710#comment:1>

Django

unread,
Jul 13, 2023, 11:15:24 AM7/13/23
to django-...@googlegroups.com
#34710: Infinite migrations using models.Choices
-------------------------------+--------------------------------------
Reporter: Div Shekhar | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: 4.2
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 Div Shekhar):

My bad - you can close this out.

I was thrown coming from django-model-utils's Choices and incorrectly
assumed TextChoices had extra features I didn't need.

--
Ticket URL: <https://code.djangoproject.com/ticket/34710#comment:2>

Django

unread,
Jul 13, 2023, 11:41:02 AM7/13/23
to django-...@googlegroups.com
#34710: Infinite migrations using models.Choices
-------------------------------+--------------------------------------
Reporter: Div Shekhar | Owner: nobody
Type: Uncategorized | Status: closed
Component: Migrations | Version: 4.2
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* status: new => closed
* resolution: => invalid


Comment:

Closing following latest comment from reporter.

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

Reply all
Reply to author
Forward
0 new messages