Home > Software design >  Unnecessary auto migrations on Django?
Unnecessary auto migrations on Django?

Time:11-11

running python 3.6 and Django 3.1.7!

So I have a Django project with an app with models as the following:

class Department(models.Model_:
   name = models.CharField(max_length=255)

class Person(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    department = models.ForeignKey(
        Department, related_name="department_members", on_delete=models.CASCADE
    )

When I run makemigrations I get this on 0001_initial.py:

class Migration(migrations.Migration):

    initial = True

    operations = [
        migrations.CreateModel(
            name='Person',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('first_name', models.CharField(max_length=255)),
                ('last_name', models.CharField(max_length=255)),
            ],
        ),
        migrations.CreateModel(
            name='Department',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=255)),
            ],
        ),
        migrations.AddField(
            model_name='person',
            name='department',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='department_members', to='my_app.department'),
        ),
    ]

That is all fine and good but now I've been getting this red text:

Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

When I run makemigrations again i get this as 0002_auto_20211111_1116.py:

class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterField(
            model_name='Department',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
        migrations.AlterField(
            model_name='Person',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
    ]

I haven't made changes to models.py and also I can't see a difference on the 'id' field that is being altered on the auto migration.

I could always leave 0002 on the folder and get on with my life but it has really been bugging me. What is giving Django the signal that something has been changed on my models? How to avoid having to create that second migration?

Thanks!

CodePudding user response:

It migrates the primary keys from BigAutoFields to AutoFields. Likely the reason for this is that since , the DEFAULT_AUTO_FIELD setting [Django-doc] is added to the settings. If you thus specified for that setting:

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

Then Django will thus use a BigAutoField.

If you then migrated back to , then Django did not take the DEFAULT_AUTO_FIELD setting into account and will use the AutoField. Since you do not mention the primary key explicitly, this thus means that it assumes that you use an AutoField, and thus will migrate the fields to an AutoField.

If you do not want this, you can remove the migration file, and explicitly define the id fields with:

class Department(models.Model):
    id = models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
   name = models.CharField(max_length=255)

class Person(models.Model):
    id = models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    department = models.ForeignKey(
        Department, related_name="department_members", on_delete=models.CASCADE
    )
  • Related