Django Pro Tips

Django Pro Tips

Django Model Changes not taking effect

When developing a new feature I created Models and then I had to completely change the model structure. When running makemigrations and migrate Django couldn't make the changes as it was quite significant.

Steps to achieve this:

1. Deleted tables from the database for the new feature that I was creating.

2. Delete migrations file that were newly added. Checking git status can reveal the newly added model files.

3. Then I did makemigrations which created the one migration file with name

4. When running migrate Django says "No migrations to apply"

5. This is because Django checks for entry in django_migrations table and then decides if there are any changes in the model.

6. You can delete the records for the particular feature model changes and migrate should take effect.

7. In case you end up deleting entire django_migrations table. Then to restore run command "python migrate --fake". This will restore all the records in django_migration table without making any database changes.

8. This will also add model record for the newly added feature model. Simply delete this record and you should be good to run "python migrate".

No Comments

Leave a Comment