[Django]-Makemigrations does not work after deleting migrations

12👍

First of all, even if this happened to you in production, do not panic.

When you deleted all migrations django forgot that this app is supposed to be managed by migrations. Django defaults back to the legacy python manage.py syncdb migrationless behaviour and will not attempt to detect changes or generate new migrations when you run python manage.py makemigrations

In order to make it aware of the migrations, you have to run the command specifically for your app:

python manage.py makemigrations appName

However, for your running application, django will not be able to detect that the new migrations were already applied in the database, and will try to run them again when you run python manage.py migrate.

When this happens the migrations fail saying that the relation appName.XYZ already exists!.

To make django understand that your migration is already reflected in the database you have to fake them:

python manage.py migrate appName --fake

This will update the migration history table and mark your migrations as applied, but will not attempt to create the tables in the database.

Update (thanks to @GwynBleiD):

One concern here is that deleted migrations which were already applied, will be listed in the migrations history table in the database. This will not be a problem for the initial python manage.py makemigrations myApp, however for any other migrations it will raise an error about inconsistent migration history.

To avoid that, you must remove by hand any row from django_migrations table in database that refers to nonexistent migrations.

Leave a comment