3

In a Django project I've been asked to remove completely an installed app;

I've already:

  • removed all code references
  • removed from INSTALLED_APPS
  • checked there are no db tables around

so far, so good; the problem raises when running:

$> python manage.py migrate

since there is a migration file around with the following:

class Migration(migrations.Migration):

    dependencies = [
        ('THE REMOVED APP', '0001_initial'),
        ...

    operations = [
        ...

It says:

django.db.migrations.exceptions.NodeNotFoundError: 
Migration xxx.yyy dependencies reference nonexistent parent node ('THE REMOVED APP', '0001_initial')

May I change the migration file and commit the following?

@@ -14,7 +14,6 @@ class Migration(migrations.Migration):

     dependencies = [
-        ('THE REMOVED APP', '0001_initial'),
     ]
Manu Artero
  • 9,238
  • 6
  • 58
  • 73
  • It depends on the contents of the migration file. If the migration has the dependency, then the migration might have a reference to that app (e.g create a foreign key that points to `other_app.DeletedModel`. Running that migration would fail because `other_app.DeletedModel` does not exist. – Alasdair Apr 17 '20 at 09:04
  • this is not the case in this particular, but would be the way to go in that case @Alasdair? – Manu Artero Apr 20 '20 at 10:35
  • 1
    You might be able to [squash the migrations](https://docs.djangoproject.com/en/3.0/topics/migrations/#squashing-migrations) to remove the references to the deleted app. – Alasdair Apr 20 '20 at 10:38

1 Answers1

-1

You can probably remove the existing migrations file and run migrations again. Note: This could be dangerous in production machines in certain conditions (check here) so choose wisely and use this solution if you are not in production.

  1. Delete *.pyc files and __pycache__ if any.
  2. Delete the migrations files and run:

    $> python manage.py makemigrations

    $> python manage.py migrate

MohitC
  • 4,541
  • 2
  • 34
  • 55
  • You don't want to delete migrations ever in case your project is already in production or used by distributed team https://stackoverflow.com/questions/28404461/can-i-delete-the-django-migration-files-inside-migrations-directory – iklinac Apr 17 '20 at 12:03
  • @iklinac answer is that depends. Here user is likely to re-install django too for solution to this – MohitC Apr 17 '20 at 12:14
  • 1
    your answer states "You can always remove the existing migrations file" , he is just removing one app/module from his project not nuking it down xD – iklinac Apr 17 '20 at 12:16