1

我们的数据库目前没有在任何表上定义主键。所有的id列都是唯一的索引。我正在删除这些索引并用正确的主键替换它们。

我的问题:在 Postgres 8.4.7 中,当我将主键添加到bigint表中时,一个表特别将数据类型从 更改为。integer

我有下表定义:

psql=# \d events
                                         Table "public.events"
        Column         |           Type           |                      Modifiers                      
-----------------------+--------------------------+-----------------------------------------------------
 id                    | bigint                   | not null default nextval('events_id_seq'::regclass)
 [more columns omitted]

Indexes:
    "events_id_unique_pk" UNIQUE, btree (id)
Foreign-key constraints:
    "events_clearing_event_ref_fk" FOREIGN KEY (clearing_event_id) REFERENCES events(id)
    "events_event_configs_id_fk" FOREIGN KEY (event_config_id) REFERENCES event_configs(id)
    "events_pdu_circuitbreaker_id_fk" FOREIGN KEY (pdu_circuitbreaker_id) REFERENCES pdu_circuitbreaker(id)
    "events_pdu_id_fk" FOREIGN KEY (pdu_id) REFERENCES pdus(id) ON DELETE CASCADE
    "events_pdu_outlet_id_fk" FOREIGN KEY (pdu_outlet_id) REFERENCES pdu_outlet(id)
    "events_sensor_id_fk" FOREIGN KEY (sensor_id) REFERENCES sensors(id)
    "events_user_id_fk" FOREIGN KEY (clearing_user_id) REFERENCES users(id)
Referenced by:
    TABLE "events" CONSTRAINT "events_clearing_event_ref_fk" FOREIGN KEY (clearing_event_id) REFERENCES events(id)
    TABLE "event_params" CONSTRAINT "events_params_event_id_fk" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
Triggers:
    event_validate BEFORE INSERT OR UPDATE ON events FOR EACH ROW EXECUTE PROCEDURE event_validate()

这就是发生的事情:

psql=# ALTER TABLE events ADD PRIMARY KEY (id);
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "events_pkey" for table "events"
ALTER TABLE
psql=# \d events
                                         Table "public.events"
        Column         |           Type           |                      Modifiers                      
-----------------------+--------------------------+-----------------------------------------------------
 id                    | integer                  | not null default nextval('events_id_seq'::regclass)
 [more columns omitted]

Indexes:
    "events_pkey" PRIMARY KEY, btree (id)
    "events_id_unique_pk" UNIQUE, btree (id)
Foreign-key constraints:
    "events_clearing_event_ref_fk" FOREIGN KEY (clearing_event_id) REFERENCES events(id)
    "events_event_configs_id_fk" FOREIGN KEY (event_config_id) REFERENCES event_configs(id)
    "events_pdu_circuitbreaker_id_fk" FOREIGN KEY (pdu_circuitbreaker_id) REFERENCES pdu_circuitbreaker(id)
    "events_pdu_id_fk" FOREIGN KEY (pdu_id) REFERENCES pdus(id) ON DELETE CASCADE
    "events_pdu_outlet_id_fk" FOREIGN KEY (pdu_outlet_id) REFERENCES pdu_outlet(id)
    "events_sensor_id_fk" FOREIGN KEY (sensor_id) REFERENCES sensors(id)
    "events_user_id_fk" FOREIGN KEY (clearing_user_id) REFERENCES users(id)
Referenced by:
    TABLE "events" CONSTRAINT "events_clearing_event_ref_fk" FOREIGN KEY (clearing_event_id) REFERENCES events(id)
    TABLE "event_params" CONSTRAINT "events_params_event_id_fk" FOREIGN KEY (event_id) REFERENCES events(id) ON DELETE CASCADE
Triggers:
    event_validate BEFORE INSERT OR UPDATE ON events FOR EACH ROW EXECUTE PROCEDURE event_validate()

我考虑了一些解决方法,但我真的很想知道它为什么会发生。还有一些其他表也使用bigint,所以我不想仅仅破解一个解决方案。

这是使用 Liquibase 编写的脚本,但它也直接在 Postgres 控制台中发生。


更新

另外两点:

  • 我可以创建一个带有bigintid 和唯一索引的简单表id,添加主键,并且列类型保持不变。
  • 执行时所有表都是空的。

它可能与约束有关吗?

4

2 回答 2

1

这很有趣。我无法用 9.1.0 版重现它(是的,我也应该升级!)。但后来我不确切知道原始表和序列是如何创建的。

此页面似乎暗示了 SERIAL 和 INTEGER 之间类型的类似自动更改:http: //grover.open2space.com/content/migrate-data-postgresql-and-maintain-existing-primary-key

会不会类似于使用 SERIAL 而不是 BIGSERIAL 创建表,然后将类型强制为 BIGINT?序列和主键操作之间的某些东西可能会重置它。

于 2012-04-16T04:06:54.083 回答
1

第二天我无法重现它,即使在它第一次发生时与目击者多次重现它之后。我把它归结为小精灵。

于 2012-06-20T16:14:31.533 回答