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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701894f066ce1-ab46eef5-98f7-4abf-863d-69ab5080f514-000000%40eu-central-1.amazonses.com.

Reply via email to