Django 上不必要的自动迁移?

Unnecessary auto migrations on Django?

运行宁 python 3.6 和 Django 3.1.7!

所以我有一个 Django 项目,其中包含一个模型如下的应用程序:

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
    )

当我 运行 makemigrations 我在 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'),
        ),
    ]

一切都很好,但现在我收到了这条红色文字:

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.

当我再次 运行 makemigrations 时,我得到这个 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'),
        ),
    ]

我没有对 models.py 进行更改,而且在自动迁移中更改的 'id' 字段上我也看不出有什么不同。

我总是可以将 0002 留在文件夹中并继续我的生活,但它确实一直困扰着我。是什么让 Django 发出我的模型发生变化的信号?如何避免必须创建第二次迁移?

谢谢!

它将主键从 BigAutoFields 迁移到 AutoFields。可能的原因是因为 , the DEFAULT_AUTO_FIELD setting [Django-doc] 被添加到设置中。如果您因此为该设置指定:

<strong>DEFAULT_AUTO_FIELD</strong> = 'django.db.models.BigAutoField'

然后 Django 将因此使用 BigAutoField.

如果您随后迁移回 ,那么 Django 不会考虑 DEFAULT_AUTO_FIELD 设置,而是会使用 AutoField。由于您没有明确提及主键,因此这意味着它假定您使用 AutoField,因此会将字段迁移到 AutoField.

如果您不想这样,您可以删除迁移文件,并明确定义 id 字段:

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

class Person(models.Model):
    id = <strong>models.BigAutoField(</strong>auto_created=True, primary_key=True, serialize=False, verbose_name='ID'<strong>)</strong>
    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
    )