1

我已经定义了一个serial PRIMARY KEY,像这样:

CREATE TABLE auth_event(
    id serial PRIMARY KEY,
    time_stamp TIMESTAMP,
    client_ip VARCHAR(512),
    user_id INTEGER REFERENCES auth_user (id) ON DELETE CASCADE,
    origin VARCHAR(512),
    description TEXT
);

我正在导入的转储具有序列字段的显式值,如下所示:

INSERT INTO "auth_event" VALUES(6,'2012-12-03 21:50:49','127.0.0.1',181,'auth','User 181 Logged-in');
INSERT INTO "auth_event" VALUES(7,'2012-12-04 07:37:43','127.0.0.1',181,'auth','User 181 Logged-in');
INSERT INTO "auth_event" VALUES(8,'2012-12-05 11:42:28','127.0.0.1',181,'auth','User 181 Logged-in');
INSERT INTO "auth_event" VALUES(9,'2012-12-07 08:01:59','127.0.0.1',181,'auth','User 181 Logged-in');
INSERT INTO "auth_event" VALUES(10,'2012-12-07 16:32:58','127.0.0.1',181,'auth','User 181 Logged-in');
INSERT INTO "auth_event" VALUES(11,'2012-12-11 15:41:24','127.0.0.1',181,'auth','User 181 Logged-in');
INSERT INTO "auth_event" VALUES(12,'2012-12-11 22:55:08','127.0.0.1',181,'auth','User 181 Logged-in');

这意味着,根据一点,序列值(与 相关的auth_event.id)将不会得到更新。

现在出现了我的问题:我使用的框架没有给序列字段赋值。这意味着 postgres 正在使用默认值,并且由于尚未更新,因此会发生冲突。

auth_event.id导入后如何更新相关的序列值?

4

1 回答 1

2
select setval(pg_get_serial_sequence('auth_event' , 'id'), (select max(id) from auth_event) );
于 2013-01-17T10:15:04.593 回答