postgresql django.db.utils.IntegrityError:重复键值违反唯一约束“django_content_type_pkey”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19135161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_content_type_pkey"
提问by JDavies
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 1.2.6 with a postgres DB.
遇到了一点问题,当我运行“ python manage.py syncdb
”时收到上述错误消息,我正在一个相当老的站点上工作。它使用 postgres 数据库运行 django 1.2.6。
The run didn't have south installed and i managed to get that working. Ran python manage.py schemamigration --initial contact_enquiries
which ran fine and asked me to migrate. I then ran python manage.py migrate contact_enquiries
I then got the same error as above.
运行没有安装南,我设法让它工作。Ranpython manage.py schemamigration --initial contact_enquiries
运行良好,并要求我迁移。然后我跑python manage.py migrate contact_enquiries
我然后得到与上面相同的错误。
It doesn't complaing about any of the syntax in my models which is why i'm confused. Here are my models and hopefully that will shed some light.
它不会抱怨我的模型中的任何语法,这就是我感到困惑的原因。这是我的模型,希望这会有所启发。
from django.db import models
class DocumentUpload(models.Model):
name = models.CharField(max_length="200")
document_upload = models.FileField(upload_to="uploads/documents")
def __unicode__(self):
return "%s" % self.name
class DocumentRequest(models.Model):
name = models.CharField(max_length="200")
company = models.CharField(max_length="200")
job_title = models.CharField(max_length="200")
email = models.EmailField(max_length="200")
report = models.ManyToManyField(DocumentUpload)
def __unicode__(self):
return "%s" % self.name
If you need anymore information, please let me know.
如果您需要更多信息,请告诉我。
Thanks!
谢谢!
回答by Wolph
Although I am not 100% certain this is the problem, there is a good chance your sequence is out of date.
虽然我不能 100% 确定这是问题所在,但很有可能您的序列已过时。
Does executing this within Postgres solve the issue?
在 Postgres 中执行此操作是否可以解决问题?
SELECT setval('django_content_type_id_seq', (SELECT MAX(id) FROM django_content_type));
回答by Komu
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 dbshellpython manage.py dbshell
2. Find the current highest value of the primary keyselect 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 290780alter sequence django_content_type_id_seq restart with 295000;
这通常意味着您的主键序列不同步。这可能是由错误的迁移等引起的。
要解决这个问题;
1. 启动 dbshell python manage.py dbshell
2. 找到主键的当前最高值select max(id) from django_content_type;
3. 更改主键序列,现在从比步骤 2 中找到的值更高的值开始。
因此假设在步骤 2 中返回的值是 290780 然后更改从大于 290780 的数字开始的序列alter sequence django_content_type_id_seq restart with 295000;
回答by jmunsch
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 inblahthing
from aOneToOneField
field to aForeignKey
- 尝试将
blahstuff
关系blahthing
从OneToOneField
字段迁移到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.
我使用的是 Django RestFramework,它通过一个ListCreateAPIView
, 和一个ModelSerializer
. 无论出于何种原因,OneToOneField
将 a 附加UNIQUE
到其中一个连接表blahmodule_blahthing_blahstuff_id
,(我实际上不确定它是否是一个连接表,这是一个假设),但是无论 Django 中发生的任何魔术切换到带有 QuerySet 返回的外键,都解决了我的问题.
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.
django_content_type_app_label_model_76bd3d3b_uniq
如果迁移在过程中失败,哈希值 和主键可能会混乱。
回答by Luke Dupin
From the inspiration of Wolph, here is my fix.
受 Wolph 的启发,这是我的解决方法。
SELECT setval('django_migrations_id_seq', (SELECT MAX(id) FROM django_migrations));