我有桌城
CREATE TABLE city
(
id serial NOT NULL,
name character varying(255) NOT NULL,
country character varying(30) DEFAULT NULL::character varying,
CONSTRAINT city_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE city
OWNER TO uks;
CREATE UNIQUE INDEX uniq_2d5b02345373c966
ON city
USING btree
(country COLLATE pg_catalog."default");
CREATE UNIQUE INDEX uniq_2d5b02345e237e06
ON city
USING btree
(name COLLATE pg_catalog."default");
现在,当我插入表格时,一切正常,序列也增加了。 当我尝试添加具有现有名称的行时因为uniqe键名称而出现错误,这是我所期望的,但在这种情况下序列也增加了:/
uks=> SELECT * FROM city;
id | name | country
----+------+---------
(0 rows)
uks=> INSERT INTO city (name) VALUES('ONE');
INSERT 0 1
uks=> INSERT INTO city (name) VALUES('TWO');
INSERT 0 1
uks=> SELECT * FROM city;
id | name | country
----+------+---------
1 | ONE |
2 | TWO |
(2 rows)
uks=> INSERT INTO city (name) VALUES('TWO');
ERROR: duplicate key value violates unique constraint "uniq_2d5b02345e237e06"
DETAIL: Key (name)=(TWO) already exists.
uks=> INSERT INTO city (name) VALUES('TWO');
ERROR: duplicate key value violates unique constraint "uniq_2d5b02345e237e06"
DETAIL: Key (name)=(TWO) already exists.
uks=> INSERT INTO city (name) VALUES('TWO');
ERROR: duplicate key value violates unique constraint "uniq_2d5b02345e237e06"
DETAIL: Key (name)=(TWO) already exists.
uks=> INSERT INTO city (name) VALUES('THREE');
INSERT 0 1
uks=> SELECT * FROM city;
id | name | country
----+-------+---------
1 | ONE |
2 | TWO |
6 | THREE |
(3 rows)
这是postgres中的正常行为吗?我认为,当错误发生时,序列不应该增加。