Recently I’ve got an error when invoking python manage.py migrate

1
2
3
4
5
Traceback (most recent call last):
File "/home/ubuntu/virt/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
DETAIL: Key (id)=(99) already exists.

This one means that you have out of date sequence within your database (improper previous migrations or jumps between different versions of code which leads to migrations being applied in chaotic order can cause this broken state). To quickly workaround this issue you can manually update values in your database. Go with python manage.py dbshell and check current values

1
2
SELECT last_value FROM django_migrations_id_seq;
SELECT last_value FROM django_content_type_id_seq;

Then update them with a command below (any sane values that are greater than ones from output above). Usually first command is enough but if you are still getting errors with key value violates unique constraint "django_content_type_pkey" you need to run second one as well (you might need to alter auth_permission_id_seq too depending on your database state)

1
2
ALTER SEQUENCE django_migrations_id_seq RESTART WITH 101;
ALTER SEQUENCE django_content_type_id_seq RESTART WITH 212;

Resources