Postgres插入一个用户定义类型的数组,里面有一个数组

Postgres insert an array of an user-defined type with an array inside

我为长度创建了这个类型:

CREATE TYPE length AS (value numeric, uom text );

然后我创建了一个长度为数组的类型

CREATE TYPE test_type AS (comp_height length[]);

在此之后,我创建了一个 table,其中有一列类型为 test_type array

CREATE TABLE test_table (test_column test_type[]);

现在我不能插入这个 table。我试过这种方法

insert into test_table (test_column)
values (
    ARRAY[
          ARRAY[(1,'m')::length,
                (20,'m')::length],
          ARRAY[(3,'m')::length,
                (40,'m')::length]
         ]::test_type
        );

但是我得到一个转换错误:(

ERROR: cannot cast type length[] to test_type

感谢任何帮助,谢谢

demo:db<>fiddle

您必须使用 ROW()length[] 转换为一条记录,它可以转换为您的新 test_type

insert into test_table (test_column)
values (
        ARRAY[   
          ROW(ARRAY[(1,'m')::length, (20,'m')::length])::test_type,
          ROW(ARRAY[(3,'m')::length, (40,'m')::length])::test_type
        ]
);