我遇到了我的主键序列与我的表行不同步的问题.
也就是说,当我插入一个新行时,我得到一个重复的键错误,因为串行数据类型中隐含的序列返回一个已经存在的数字.
这似乎是由导入/恢复不能正确维护序列引起的.
我在postgres中有下表:
CREATE TABLE "test" (
"id" serial NOT NULL PRIMARY KEY,
"value" text
)
Run Code Online (Sandbox Code Playgroud)
我正在做以下插入:
insert into test (id, value) values (1, 'alpha')
insert into test (id, value) values (2, 'beta')
insert into test (value) values ('gamma')
Run Code Online (Sandbox Code Playgroud)
在前2个插入中,我明确提到了id.但是,在这种情况下,表的自动增量指针不会更新.因此在第3个插入中我得到错误:
ERROR: duplicate key value violates unique constraint "test_pkey"
DETAIL: Key (id)=(1) already exists.
Run Code Online (Sandbox Code Playgroud)
我从未在MyISAM和INNODB引擎中的Mysql中遇到过这个问题.显式与否,mysql总是根据最大行id更新自动增量指针.
postgres中这个问题的解决方法是什么?我需要它,因为我希望对我桌子中的某些ID进行更严格的控制.
更新: 我需要它,因为对于某些值我需要有一个固定的ID.对于其他新条目,我不介意创建新的条目.
我认为可以通过在我明确插入id时手动递增nextval指针来实现max(id) + 1.但我不知道该怎么做.
嗨,我有一个问题,我知道这是多次发布,但我没有找到我的问题的答案.问题是我有一个表和一列"id"我希望它是正常的唯一数字.这种类型的列是串行的,每个插入后的下一个值是从一个序列中提交的,所以一切似乎都可以,但它仍然有时显示此错误.我不知道为什么?在文档中它是写的顺序是傻瓜教授,并始终有效.如果我向该列添加UNIQUE约束,那么它会是什么?我曾多次在Postres工作,但这个错误第一次出现在我面前.我做的一切正常,我之前从未遇到过这个问题.你能帮我找到可以在将来用于所有将要创建的表的答案吗?让我们说我们有这样的容易:
CREATE TABLE comments
(
id serial NOT NULL,
some_column text NOT NULL,
CONSTRAINT id_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE interesting.comments OWNER TO postgres;
Run Code Online (Sandbox Code Playgroud)
如果我添加:
ALTER TABLE comments ADD CONSTRAINT id_id_key UNIQUE(id)
Run Code Online (Sandbox Code Playgroud)
是否应该或是否还有其他事情要做?
卡住了我有一个数据库,当我尝试使其python manage.py migrate出现以下错误时:
django.db.utils.IntegrityError: duplicate key value violates unique constraint "auth_permission_pkey"
DETAIL: Key (id)=(241) already exists.
Run Code Online (Sandbox Code Playgroud)
以下是整个错误:
Operations to perform:
Apply all migrations: admin, auth, companyapp, contenttypes, djcelery, kombu_transport_django, loginapp, projectmanagement, recruitmentproject, sessions, smallproject
Running migrations:
No migrations to apply.
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/ubuntu/.local/lib/python2.7/site-packages/django/core/management/base.py", line 330, …Run Code Online (Sandbox Code Playgroud) 我正在将 PostgreSQL 与 Django 结合使用,并且尝试使用 Django ORM 从数据库中获取一些对象。
Medicine.objects.get(unique_item_id=26775)
Run Code Online (Sandbox Code Playgroud)
但是在获取时我遇到了错误 ->item_medicine.models.DoesNotExist: Medicine matching query does not exist.
然后我尝试使用 Django ORM 插入相同的内容。
Medicine.objects.create(unique_item_id=26775)
Run Code Online (Sandbox Code Playgroud)
但我再次收到错误psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "item_medicine_medicine_unique_item_id_key" DETAIL: Key (unique_item_id)=(26775) already exists.
在我的模型中,我添加了unique=True字段unique_item_id。
我不知道为什么会发生这种情况。我尝试了类似帖子中给出的答案,但没有任何效果。
追溯:
Traceback (most recent call last):
File "/home/rohit/virtualenvs/envmedicine/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "item_medicine_medicine_unique_item_id_key"
DETAIL: Key (unique_item_id)=(26775) already exists.
The above exception was the direct cause of the …Run Code Online (Sandbox Code Playgroud) django postgresql django-models django-orm unique-constraint