coderholic / django-cities

Countries and cities of the world for Django projects
MIT License
930 stars 374 forks source link

Error when reverting migrations #198

Open leibowitz opened 6 years ago

leibowitz commented 6 years ago

Checklist

Steps to reproduce

python manage.py migrate cities 0001_initial

Expected behavior

No errors

Actual behavior

psycopg2.IntegrityError: column "continent_code" contains null values

Full output (including Traceback): https://gist.github.com/leibowitz/0427c3e70ffb07d81e1ecf82b349cf00

This is due to the last RemoveField of the 0002_continent_models_and_foreign_keys.py migration

As the documentation mentions:

The operation is reversible (apart from any data loss, which of course is irreversible) if the field is nullable or if it has a default value that can be used to populate the recreated column. If the field is not nullable and does not have a default value, the operation is irreversible.

Source: https://docs.djangoproject.com/en/2.0/ref/migration-operations/#removefield

Adding default='' to the Country.continent field definition https://github.com/coderholic/django-cities/blob/422e92d9ceaa07444d8fd6de00a526c72bf8e9ca/cities/migrations/0001_initial.py#L64 solves the issue.

I imagine retrospectively adding a migration between 0001 and 0002 (to add default='') would have prevented this issue, but it is a bit too late to do so.

The only way, in my opinion, is to add the default='' to the original definition - which shouldn't impact anyone already using django-cities.

https://github.com/coderholic/django-cities/pull/199