Search code examples
djangodjango-modelsmanage.py

Django manage.py makemigrations: Show differences between model and database


I occasionally run into a problem where manage.py makemigrations will generate migrations that, as far as I can tell, concern models or model fields I didn't touch. Is there any way to find out what differences between the model and the database manage.py makemigrations found, that make it think a migration is needed?

I searched the web and checked the django-admin and manage.py documentation but couldn't find a way to make makemigrations "explain itself" or to find differences between my models and database any other way.

Any hints would be greatly appreciated :)


Solution

  • python manage.py makemigrations --dry-run

    https://docs.djangoproject.com/en/5.0/ref/django-admin/#cmdoption-makemigrations-dry-run

    Edit:

    I think there's some confusion here. Make migrations does not compare with the state of the database as such. It compares with the state of the models if you ran all existing migrations from scratch for that model.

    So if the current migration gives you eg a CharField with length 30, and your current model definition has changed that to 32, makemigrations will give you the field change to 32.

    So the migration you created (or the migration it shows you with --dry-run - same thing) - is the difference between what the DB should look like now and what your model looks like, assuming nobody has done any manual changes in the DB (which you absolutely should not if you are using a migration suite like the one django provices).