无法在 postgres 数据库表中插入新行? [英] Can't insert new row into postgres database table?

查看:72
本文介绍了无法在 postgres 数据库表中插入新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我试图在 postgres 数据库表中插入一个新行,但出现以下错误

I have an issue, I'm trying to insert a new row into a postgres database table and get the following error

ERROR:  duplicate key violates unique constraint "n_clients_pkey"

这里是我的查询

insert into n_clients(client_name) values( 'value');

我使用的是 postgres 8.1.11

I'm using postgres 8.1.11

 PostgreSQL 8.1.11 on x86_64-redhat-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20070626 (Red Hat 4.1.2-14)

这是我的表的结构

                                                Table "public.n_clients"
   Column    |           Type           |                               Modifiers                               
-------------+--------------------------+-----------------------------------------------------------------------
 id          | integer                  | not null default nextval(('public.n_clients_id_seq'::text)::regclass)
 client_name | character varying(200)   | not null
 moddate     | timestamp with time zone | default now()
 createdate  | timestamp with time zone | default now()
Indexes:
    "n_clients_pkey" PRIMARY KEY, btree (id)

和序列

Sequence "public.n_clients_id_seq"
    Column     |  Type   
---------------+---------
 sequence_name | name
 last_value    | bigint
 increment_by  | bigint
 max_value     | bigint
 min_value     | bigint
 cache_value   | bigint
 log_cnt       | bigint
 is_cycled     | boolean
 is_called     | boolean

推荐答案

此行已存在,因此无法插入.你关系的主键是什么?是序列吗?如果是这样,也许它卡住了(也许你导入了数据).您应该手动将其重置为下一个可用的空闲 ID(例如,如果最大 ID 为 41,您应该执行:SELECT setval('your_seq', 42);) 然后再试一次.

This row exists already, therefore you cannot insert it. What is the primary key of your relation? Is it a sequence? If so, maybe it got stuck (maybe you imported data). You should reset it manually to the next free ID available (e.g., if the maximum ID is 41, you should do: SELECT setval('your_seq', 42);) then try again.

这篇关于无法在 postgres 数据库表中插入新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆