【发布时间】:2018-11-20 07:46:47
【问题描述】:
当插入带有隐式主键的行时,它似乎不会影响主键序列,然后,当尝试在没有 PK 的情况下插入时,它会失败:
create table testtable(
id serial primary key,
data integer not null
);
使用 PK 插入(例如关于数据迁移):
insert into testtable ( id, data ) values ( 1,2 ), ( 2,2 ), ( 3,2 ), ( 4,2 );
INSERT 0 4
插入新数据,不带PK:
insert into testtable ( data ) values ( 4 ), ( 5 ), ( 6 ), ( 7 );
ERROR: duplicate key value violates unique constraint "testtable_pkey"
DETAIL: Key (id)=(1) already exists.
为什么在第一个INSERT 之后没有在最大值上设置序列?我应该在插入 PK 后控制序列吗?有没有办法让序列自动在正确的轨道上?
【问题讨论】:
标签: postgresql primary-key postgresql-9.5