Diego Puente
Diego Puente

Reputation: 2004

Uninstall app A that app B have a dependency in one old migration

I am trying to uninstall an app named django-cities but in my app "places" I have a model called Venue, that in the migration 0001_initial.py had a ForeingKey to cities.Subregion model of django-cities.

I proceeded to delete django-cities of INSTALLED_APPS but I got the following error:

Traceback (most recent call last):
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 128, in inner_run
    self.check_migrations()
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/core/management/base.py", line 422, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 52, in __init__
    self.build_graph()
  File "/home/d/.virtualenvs/beplay/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 274, in build_graph
    raise exc
django.db.migrations.exceptions.NodeNotFoundError: Migration places.0001_initial dependencies reference nonexistent parent node (u'cities', u'0010_adjust_unique_attributes')

Then I deleted those dependencies and uninstall django-cities and all worked for me, but if anyone else has to install the project, the migrate command raises the following error:

ValueError: Related model u'cities.Subregion' cannot be resolved

because I deleted from requirements.txt and it is still referenced in migration 0001_initial.py:

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Venue',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, editable=False, verbose_name='created')),
                ('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, editable=False, verbose_name='modified')),
                ('name', models.CharField(max_length=255)),
                ('phone', models.CharField(blank=True, max_length=255, null=True)),
                ('mobile', models.CharField(blank=True, max_length=255, null=True)),
                ('email', models.EmailField(blank=True, max_length=254, null=True)),
                ('address', models.CharField(blank=True, max_length=255, null=True)),
                ('latitude', models.CharField(blank=True, max_length=100, null=True)),
                ('longitude', models.CharField(blank=True, max_length=100, null=True)),
                ('subregion', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='cities.Subregion')),
            ],
            options={
                'abstract': False,
            },
        ),
    ]

Then I delete the line:

('subregion', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='cities.Subregion')),

and have another error:

AttributeError: 'NoneType' object has no attribute 'is_relation'

I also tried to delete all .pyc files from the project, also I googled this error and found this, but it didn't provided an answer.

Any info about this?

Thanks and sorry for my bad English.

Upvotes: 4

Views: 2242

Answers (1)

John Moutafis
John Moutafis

Reputation: 23154

There are two possible solutions:

Note: For both the following solutions you need to drop the old Venue table from your database before you proceed.

The easy one:

  • Go to your migrations/ folder and delete everything except the __init__.py file.

  • Delete your app from the INSTALLED_APPS.

  • Run python manage.py makemigrations which will recreate your migrations in the folder.

  • Run python manage.py migrate

    Drawbacks: You loose your migration history if that matters (in your case I will assume that it does not matter since you refer to migration 0001)

The hard way:

You will need to modify every migration file in your migrations/ folder:

  • Go to every migration file and find any reference to the app that will get uninstalled
  • Delete those references:

    Example delete the line:

    ('subregion', models.ForeignKey(
                      blank=True, 
                      null=True,
                      on_delete=django.db.models.deletion.CASCADE,
                      to='cities.Subregion'
                  ))
    

    from the Venue table fields migration.

  • Delete your app from the INSTALLED_APPS.

  • Run python manage.py migrate

    Drawbacks: It is complicated and prone to mistakes.

Upvotes: 5

Related Questions