添加create table
语句来复制问题:
CREATE TABLE table1
(
id serial NOT NULL,
"timestamp" timestamp with time zone,
dir integer,
created timestamp without time zone,
deleted boolean DEFAULT false,
CONSTRAINT pk_table1_id PRIMARY KEY (id)
);
CREATE TABLE table2
(
id serial NOT NULL,
"timestamp" timestamp with time zone,
dir integer,
created timestamp without time zone,
deleted boolean DEFAULT false,
CONSTRAINT pk_table2_id PRIMARY KEY (id)
);
insert into table1 (timestamp, dir, created) values('2015-01-01 17:52:00', 3, now()), ('2015-01-02 17:52:00', 1, now()), ('2015-01-03 17:52:00', 1, now());
insert into table2
select * from table1
insert into table2 (timestamp, dir, created) values('2015-01-04 17:52:00', 3, now());
错误:重复键值违反了唯一约束" pk_table2_id" DETAIL:Key(id)=(1)已经存在。
我正在尝试使用以下内容重置postgres中新创建的表的序列计数器:
SELECT setval('table2_id_seq', (SELECT MAX(id) FROM table2));
setval返回3.
但是没有设置计数器,因为我的下一个插入语句失败,出现以下错误:
insert into table2 (timestamp, dir, created) values('2015-01-04 17:52:00', 3, now());
错误:重复键值违反了唯一约束" pk_table2_id" DETAIL:Key(id)=(2)已经存在。
select version();
在x86_64-apple-darwin14.5.0上的PostgreSQL 9.4.5,由Apple LLVM版本7.0.0(clang-700.0.72)编译,64位