Django migrations reference a deleted module

There are two cases:

  1. You moved FooModel elsewhere, then edit all your migration files to reflect that move.

  2. You removed FooModel, in this case, follow these steps:

    • Put FooModel back to where it was.
    • Make sure there are no references to FooModel elsewhere in your code.
    • Run ./manage.py makemigrations my_app
    • Run ./manage.py squashmigrations my_app <migration created with the previous comand> — see the doc for more informations on squashing migrations.
    • Repeat the two previous steps for any app that references FooModel in its migrations.
    • Remove FooModel and the stale migration files once you ensured everything worked fine.

This should work because as FooModel is not referenced from any other model, it should be removed from the migrations files when squashing them.

However, be warned that squashing migrations is not a simple operation and may have consequences it might be a better idea to just keep the model in your codebase without using it.

Note: in this situation, the object in question is a Django model but this applies to any class, function or module referenced by migration files.