Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled pending operations for models when trying to perform migration

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),
        ),
    ]
like image 719
segalle Avatar asked Dec 14 '15 12:12

segalle


2 Answers

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!

like image 196
segalle Avatar answered Oct 19 '22 23:10

segalle


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.

like image 42
Darlin Alberto Avatar answered Oct 20 '22 01:10

Darlin Alberto