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

前端 未结 5 1761
春和景丽
春和景丽 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

    I received this error:

    django.db.utils.IntegrityError: duplicate key value violates unique constraint 
        "blahmodule_blahthing_blahstuff_id"
    DETAIL:  Key (blahstuff_id)=(1) already exists.
    

    A possible solution:

    • Try migrating the blahstuff relation in blahthing from a OneToOneField field to a ForeignKey

    An explanation with what I was using:

    I was using the Django RestFramework which was wired up through a ListCreateAPIView, and a ModelSerializer. For whatever reason the OneToOneField attaches a UNIQUE to one of the join tables blahmodule_blahthing_blahstuff_id, ( i'm not actually sure if its a join table, this is an assumption ), but whatever magic is happening in django switching to a ForeignKey with QuerySet returns, solved my issue.

    related:

    • What's the difference between django OneToOneField and ForeignKey?
    • https://www.postgresql.org/docs/11/ddl-constraints.html

    As far as the actual question above:

    schema :

    -- Table: public.django_content_type
    
    -- DROP TABLE public.django_content_type;
    
    CREATE TABLE public.django_content_type
    (
        id integer NOT NULL DEFAULT nextval('django_content_type_id_seq'::regclass),
        app_label character varying(100) COLLATE pg_catalog."default" NOT NULL,
        model character varying(100) COLLATE pg_catalog."default" NOT NULL,
        CONSTRAINT django_content_type_pkey PRIMARY KEY (id),
        CONSTRAINT django_content_type_app_label_model_76bd3d3b_uniq UNIQUE (app_label, model)
    
    )
    WITH (
        OIDS = FALSE
    )
    TABLESPACE pg_default;
    
    ALTER TABLE public.django_content_type
        OWNER to YOURUSER;
    

    Example rows:

    pk    app_label                model
    
    1   "your_app"           "your_app_model"
    2   "your_app"           "your_app_model"
    3   "blah_module"        "blahthing"
    4   "blah_module"        "blahstuff"
    

    The hash on django_content_type_app_label_model_76bd3d3b_uniq and the Primary Keys can get jumbled up if a migration fails mid process.

    0 讨论(0)
  • 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};
    
    0 讨论(0)
  • 2020-12-31 01:24

    This usually means that your primary key sequence has gone out of sync. This may have been caused by a bad migration etc..
    To fix this;
    1. Start the dbshell
    python manage.py dbshell
    2. Find the current highest value of the primary key
    select max(id) from django_content_type;
    3. Change the primary key sequence to now start at a value higher than the value found in step 2.
    So assuming the value returned in step 2 was 290780 then alter sequence to start at a number greater than 290780
    alter sequence django_content_type_id_seq restart with 295000;

    0 讨论(0)
  • 2020-12-31 01:30

    Although I am not 100% certain this is the problem, there is a good chance your sequence is out of date.

    Does executing this within Postgres solve the issue?

    SELECT setval('django_content_type_id_seq', (SELECT MAX(id) FROM django_content_type));
    
    0 讨论(0)
  • 2020-12-31 01:35

    From the inspiration of Wolph, here is my fix.

    SELECT setval('django_migrations_id_seq', (SELECT MAX(id) FROM django_migrations));
    
    0 讨论(0)
提交回复
热议问题