django.db.utils.IntegrityError: duplicate key value violates unique constraint “django_content_type_pkey”

前端 未结 5 1795
春和景丽
春和景丽 2020-12-31 00:43

Ran into a bit of a problem, i\'m getting the above error message when i run \'python manage.py syncdb\' I\'m working on a fairly old site. It\' running django

5条回答
  •  青春惊慌失措
    2020-12-31 01:18

    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.

    python manage.py dbshell
    

    check the current value of following table

    SELECT last_value FROM django_migrations_id_seq;
    SELECT last_value FROM django_content_type_id_seq;
    

    Then update them with the command below (any sane values that are greater than ones from the output above). Usually, the 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 the second one as well (you might need to alter auth_permission_id_seq too depending on your database state)

    ALTER SEQUENCE django_migrations_id_seq RESTART WITH {value_greater_than_last_value};
    ALTER SEQUENCE django_content_type_id_seq RESTART WITH {value_greater_than_last_value};
    

    Even after this, you are getting error. You use

    SELECT last_value FROM auth_permission_id_seq;
    

    Increment value by one

    ALTER SEQUENCE auth_permission_id_seq RESTART WITH {value_greater_than_last_value};
    

提交回复
热议问题