Solving IrreversibleError on migrations of third party Django apps

Luan Junio Pereira Bittencourt
2 min readAug 6, 2022

--

A while ago i wrote a Django app that implements a swappable model for a third party app. By accident i runned the migrations before adding a “run_before” clause referencing the third party app migrations on my custom migration. To resolve this issue a tried to reverse the third party app migrations and for my surprise i got a django.db.migrations.exceptions.IrreversibleError. That occurs because the third party app runs a RunPython migration operation without defining a reverse_code to unapply the migration.

Even though the mistake only affected a development environment i came across a solution. To start i cloned the git repo of the third party app and copied it’s migration folder contents to a folder called “temp_migrations” under my custom application folder. In the file of the migrations that got the error i added a reverse_code (i used RunPython.noop, but you may define a custom function).

Before unapplying the migrations i added the following line to my settings.py file. MIGRATION_MODULES is a dictionary specifying the package where migration modules can be found on a per-app basis.

Then i unnaplied the migrations for my custom app and for the thirdy party app.

To finish i removed the temp_migrations folder and the MIGRATION_MODULES setting. To prevent problems with the swappable model added a “run_before” clause to my custom migration before running it.

I already wrote a PR to the third party app repo to prevent other people from facing the same problem with the irreversible migration :)

--

--