coderholic/django-cities

Error when reverting migrations

leibowitz opened this issue · 0 comments

Checklist

  • I have verified that I am using a GIS-enabled database, such as PostGIS or Spatialite.
  • I have verified that that issue exists against the master branch of django-cities.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

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

('continent', models.CharField(max_length=2)),
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.

#199