Migration dependency not added when field is altered

22 views
Skip to first unread message

Erin Masatsugu

unread,
May 27, 2020, 10:02:34 PM5/27/20
to Django users
If I have this a model in app A:
class AppAModel(models.Model):
    name = models.CharField(max_length=50, default="")

and this model in app B:
class AppBModel(models.Model):
    name = models.CharField(max_length=50, default="")

and a I add a foreign key to AppAModel in app C:
class AppCModel(models.Model):
    my_field = models.ForeignKey(AppAModel, on_delete=models.CASCADE)

the generated migration file correctly adds a dependency from app C to app A:
class Migration(migrations.Migration):

    dependencies = [
        ('app_a', 'XXXX_app_a_migration'),
        ('app_c', 'XXXX_app_c_migration'),
    ]

    operations = [
        migrations.CreateModel(
            name='AppCModel',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('my_field', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ncm.AppAModel')),
            ],
        ),
    ]

However, if I then update my_field to instead have a foreign key to AppBModel, django doesn't add a migration dependency from app C to app B:
class Migration(migrations.Migration):

    dependencies = [
        ('app_c', 'XXXX_app_c_migration'),
    ]

    operations = [
        migrations.AlterField(
            model_name='appcmodel',
            name='my_field',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='metals.AppBModel'),
        ),
    ]

I saw the response here https://groups.google.com/forum/#!searchin/django-users/migration$20dependency%7Csort:date/django-users/-h9LZxFomLU/ry_yeWGfDQAJ so I don't believe this is expected behavior, but can someone confirm?

Thank you!
Erin

Durai pandian

unread,
May 28, 2020, 12:44:16 AM5/28/20
to django...@googlegroups.com
When you make any changes to the field, AlterField will be added rather than AddField.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/48ab3732-f667-472f-a678-ca9f082315da%40googlegroups.com.


--
Thanks & Regards,
Durai pandian
Mobile  :
+91-9611051220
Email    : ddp...@gmail.com

Erin Masatsugu

unread,
May 28, 2020, 10:46:52 AM5/28/20
to Django users
I understand that -- my question is why the AlterField operation in this case didn't add a migration dependency.


On Wednesday, May 27, 2020 at 6:44:16 PM UTC-10, Durai pandian wrote:
When you make any changes to the field, AlterField will be added rather than AddField.

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages