【问题标题】:Postgres insert an array of an user-defined type with an array insidePostgres插入一个用户定义类型的数组,里面有一个数组
【发布时间】:2021-05-28 18:22:49
【问题描述】:

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

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

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

CREATE TYPE test_type AS (comp_height length[]);

在此之后,我创建了一个包含 test_type 数组类型列的表

CREATE TABLE test_table (test_column test_type[]);

现在我无法插入此表。这种方式我试过了

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

感谢您的帮助,谢谢

【问题讨论】:

    标签: postgresql user-defined-types composite-types


    【解决方案1】:

    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
            ]
    );
    

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2015-09-19
      相关资源
      最近更新 更多