python - PostgreSQL 错误 : insert operation returns id exists in Python

标签 python postgresql

<分区>

我试图将 name 插入数据库。

name = 'test'
cur.execute("INSERT INTO scholars(name) VALUES('{}') returning id".format(name))
id = cur.fetchone()
print(id)

错误信息:

psycopg2.IntegrityError: duplicate key value violates unique constraint "idx_16514_primary"
DETAIL:  Key (id)=(2321) already exists.

id=2301时,成功,返回ID。 之后,id += 1并返回以下错误。

http://ob9j09f06.bkt.clouddn.com/2016-10-12-16%3A31%3A12.jpg

最佳答案

在我看来,您的数据库中已经有 ID 为 2321 的记录。你可以用 SELECT id FROM scholars 检查一下吗?

关于python - PostgreSQL 错误 : insert operation returns id exists in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39998053/

相关文章:

PostgreSQL C 函数 : libpq doesn't link?

json - Postgres 中从 JSON 更新记录的函数

python - RGB 图像的像素强度以及如何将其与整数相乘以查看灰色阴影

python - mypy 给出错误,类型不兼容 "Optional[int]";预期“Union[SupportsFloat、str、bytes、bytearray]

python - 尝试使路径工作 - 尝试超越顶级包的相对导入

java - @Column 长度大于 255 个字符的实体

sql - 查找表中聚合值之间的最大值

python - 列出 Web 服务 UpdateList 失败并显示 "Attempted to use an object that has ceased to exist"

python - django rest framework 如何国际化领域

django - 我可以使用哪些命令/配置来使用 ecs-cli 部署 django+postgres+nginx?