首页 > 解决方案 > 无法更改列以更改数据类型,因为继承了列

问题描述

我创建了一个名为的表trackings和许多分区子表,它们被命名为trackings_pt_....

分区表trackings_pt_... 继承自该trackings表。

我想将id所有这些(父表和子表)的列从integer更改为bigint.

Alter table trackings alter column id set data type bigint;

我面临

ERROR: cannot alter inherited column "id"

请帮我解决它。

Postgres 版本:x86_64-apple-darwin17.7.0 上的 PostgreSQL 11.2,由 Apple LLVM 版本 10.0.0 (clang-1000.11.45.5) 编译,64 位

标签: postgresqldatabase-partitioning

解决方案


您必须尝试更改子表上的数据类型,因为它适用于父表:

CREATE TABLE inh_parent (id integer NOT NULL, val text NOT NULL);

CREATE TABLE inh_child () INHERITS (inh_parent);

CREATE TABLE inh_child2 (id integer NOT NULL) INHERITS (inh_parent);
NOTICE:  merging column "id" with inherited definition

ALTER TABLE inh_parent ALTER id TYPE bigint;  -- works

ALTER TABLE inh_child ALTER id TYPE int;      -- fails
ERROR:  cannot alter inherited column "id"

trackings必须从另一个表继承。跑\d trackings进去psql看看。


推荐阅读