Search code examples
djangopostgresqldjango-migrations

django migrations does not update database


I have django app already online and running. I created new model and if I do the makemigrations, it is working fine, but if I want to migrate it, then I receive Running migrations:No migrations to apply. Even though that I made a change. The change is also in the migration file. I am using postgresql with it. I tried to delete the migration file and create it once more, but it did not help. Any idea what might be wrong?


Solution

  • Looks like some migrations are marked as applied incorrectly in the database. This can be solved by using the --fake flag [Django docs] to mark an appropriate migration as applied / some migrations as unapplied.

    Suppose that your app is named myapp and the migration 0004 is applied properly to the database (you are trying to apply 0005 or further which doesn't work) then you will run the following command:

    python manage.py migrate myapp 0004 --fake
    

    Note: myapp and 0004 is an example, do this according to your apps name and which migrations are applied.

    Edit: It appears that you have deleted the migrations. But thankfully have some version control in place. Best would be to get the migration files back from the version control. If for some reason you are unable to do that, revert the models to how they were before their changes and make sure no migration files exist for now. Next generate the migration files by running makemigrations (check that they are as expected and your database state corresponds to these generated files). Next run the following two commands:

    python manage.py migrate <appname> zero --fake
    python manage.py migrate <appname> --fake
    

    The first one will mark all migrations as unapplied for the app. The second one will mark all the migrations as applied. Next you will add the changes you wanted to make back to the models and migrate again.