将列数据类型从数组更改为整数

时间:2019-06-25 11:29:42

标签: postgresql ddl

我需要将列的数据类型从_float8更改为int4

ALTER TABLE "table" ALTER COLUMN "col" SET DATA TYPE int4;

得出column "col" cannot be cast automatically to type integer

ALTER TABLE "table" ALTER COLUMN "col" SET DATA TYPE int4 USING (col::integer);

得出cannot cast type double precision[] to integer

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您必须指出转换中应使用数组的哪个元素,例如

alter table x alter column y set data type int4 using (y[1]::int)

db<>fiddle.

答案 1 :(得分:0)

问题是您有一个 array 。要解决此问题,您需要使用数组运算符:

ALTER TABLE "table"
    ALTER COLUMN "col" SET DATA TYPE int4 USING (col[1]::integer);

Here是db <>小提琴。