37

I have observed this behaviour on version 1.7 but not in previous versions using south migration.

eg.

class RedemptionCode(models.Model):
    EXPIRE_OPTIONS = (
        ('1 week', '1 Week'),
    )

    expire_option = models.CharField(max_length=255, choices=EXPIRE_OPTIONS)

when I added more options:

EXPIRE_OPTIONS = (
    ('1 week', '1 Week'),
    ('2 weeks', '2 Weeks'),
    ('1 month', '1 Month'),
    ('1 day', '1 Day'),
)

and run makemigrations, it creates a migration for it, coming from south background I thought it should say no changes detected as it doesn't affects database schema. I don't know what purpose it serves:

class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterField(
            model_name='redemptioncode',
            name='expire_option',
            field=models.CharField(max_length=255, choices=[('1 week', '1 Week'), ('2 weeks', '2 Weeks'), ('1 month', '1 Month'), ('1 day', '1 Day')]),
        ),
    ]
James Lin
  • 25,028
  • 36
  • 133
  • 233
  • since I got a couple of up-votes, seems to me this is not documented, hence I created a ticket https://code.djangoproject.com/ticket/23586 – James Lin Oct 02 '14 at 22:06
  • 1
    Look at my answer for similar problem http://stackoverflow.com/a/29233569/3025188 Cheers! – user3025188 Mar 28 '15 at 11:23
  • For anybody else this blog post http://tech.yunojuno.com/pro-tip-django-choices-and-migrations demonstrates a good solution. – Peter Bengtsson May 09 '19 at 18:59

2 Answers2

37

After raised the ticket and got closed due to duplication, finally found the answer:

This is by design. There are several reasons, not least of which for me that datamigrations at points in history need to have a full accurate representation of the models, including all their options not just those which affect the database.

Reference:

James Lin
  • 25,028
  • 36
  • 133
  • 233
4

From Django docs:

Django will make migrations for any change to your models or fields - even options that don’t affect the database - as the only way it can reconstruct a field correctly is to have all the changes in the history, and you might need those options in some data migrations later on (for example, if you’ve set custom validators).

Reference:

Igor Alex
  • 841
  • 2
  • 10
  • 21