[Django]-Django migrations

3đź‘Ť

âś…

You’re seeing this error message because django is trying to build a consistent history of migrations, and it complains that if there was a database that held data with your old migrations, and you’d try to add a non-nullable field, it wouldn’t know what to do.

Migrations are supposed to be put into version control and used across different development/production environments. If you add a field that must not be null, existing data of other environments (for example a production database that held models that did not have the field post_id) then django will warn you about this, with the error message that you got, and offer two solutions:

  1. This field should always be prepoulated with a default value( you have to modify the models.py)

  2. This is a one time migration and you supply a one-off value, for example “LEGACY” to mark pre-migration data.

If you’re not in production and there is no valuable data on your development server, an easy way to fix this error message is just to delete the existing migration files and run python manage.py makemigrations && python manage.py migrate again.

Leave a comment