Unhandled pending operations for models when trying to perform migration
Asked Answered
B

2

5

When I perform a migration on one of my projects app I get the following error:

ValueError: Unhandled pending operations for models: common.shipmentaddress (referred to by fields: catalog.Fulfillment.address)

Django 1.9, python 2.7.10

I was looking for cyclic import but i don't think this is it

These are the models:

class ShipmentAddress(models.Model):
    recipient_first_name = models.CharField(max_length=50, null=True, blank=True)
    recipient_last_name = models.CharField(max_length=50, null=True, blank=True)
    street_name = models.CharField(max_length=50)
    state = models.ForeignKey(State)
    postal_code = models.IntegerField(default=0)
    city = models.CharField(max_length=50)

    class Meta:
        db_table = 'shipment_address'


class Fulfillment(models.Model):
    address = models.ForeignKey(ShipmentAddress)
    inventory_items = models.ManyToManyField(Item_With_Size, through='Inventory_Item')

    class Meta:
        verbose_name = 'fulfilment'
        verbose_name_plural = 'fulfilments'
        db_table = 'fulfilment'

The migrations looks like that:

class Migration(migrations.Migration):

    dependencies = [
        ('catalog', '0009_auto_20151130_1118'),
     ]

    operations = [
        migrations.AlterField(
            model_name='fulfillment',
            name='address',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='common.ShipmentAddress'),
        ),
    ]

class Migration(migrations.Migration):

    dependencies = [
        ('common', '0005_shipmentaddress'),
    ]

    operations = [
        migrations.RenameField(
            model_name='shipmentaddress',
            old_name='recipient_name',
            new_name='recipient_first_name',
        ),
        migrations.AddField(
            model_name='shipmentaddress',
            name='recipient_last_name',
            field=models.CharField(blank=True, max_length=50, null=True),
        ),
    ]
Bik answered 14/12, 2015 at 12:36 Comment(3)
Could you post the offending models definition?Philippines
Could you post the migrations for both the common and catalog apps, and the full traceback?Krasnoyarsk
I edit the question the actual migration i do is on other app (none of the above)Bik
B
4

Ok I got it!

It seems that the migration process went over all of my previous migrations and not only on the last one... in one of the previous migrations there was a wrong Foreign key pointer that caused that problem

I fixed that old migration and thats it!

Bik answered 16/12, 2015 at 10:8 Comment(4)
That's silly. Django breaks migrations when a model name is changed because of this behaviour.Pentose
@MJafarMash Did you find a workaround on this? I changed model names and I have some related ManyToMany fields and it breaks with this error message.Mikol
@Mikol I changed models names in old migrations. But it makes old migrations useless for other peoplePentose
@MJafarMash you are right, fortunately i recently did migration reset so no one will use these againBik
B
3

For those renaming a model referenced in a Django ForeignKey, a different solution I found for this problem is redefining the field in the later migration. This avoids having to edit existing migrations.

If you have the following operation (automatically-added) in the first migration:

    migrations.AddField(
                model_name='my_model',
                name='my_fk',
                field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='my_app.old_model_name'),
            )

In the migration where old_model_name is renamed to new_model_name manually add the following operation:

    migrations.AlterField(
                model_name='my_model',
                name='my_fk',                
                field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='my_app.new_model_name'),
            )

The differences between the two is call to AlterField instead of AddField and having to reference the new model name in the ForeignKey field.

This was tested on Django 1.9.12.

Breathtaking answered 6/12, 2016 at 19:39 Comment(1)
A year after my original post, the best way is to make sure that the dependencies are in the right order... and then you wont need to rename anything cheersBik

© 2022 - 2024 — McMap. All rights reserved.