添加主键更改列类型

时间:2012-02-28 23:59:46

标签: postgresql liquibase

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

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

我有以下表格定义:

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控制台中发生。


更新

另外两点:

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

它可能与约束有关吗?

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

第二天,即使第一次与目击者一起复制多次,我也无法重现这一点。我正在把它变成粉红色。