﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34710	Infinite migrations using models.Choices	Div Shekhar	nobody	"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'
}}}
"	Uncategorized	closed	Migrations	4.2	Normal	invalid			Unreviewed	0	0	0	0	0	0
