【问题标题】:Are custom domains arrays possible in PostgreSQL 12?PostgreSQL 12 中是否可以使用自定义域数组?
【发布时间】:2020-12-15 01:12:21
【问题描述】:

我已将自定义域 tmoney 声明为

create domain tmoney as decimal (13,4);

然后我在表声明中使用它的数组,

create table test (
  id        int generated by default as identity primary key,
  volume    smallint[5] not null default '{0, 0, 0, 0, 0}',
  price     tmoney[5]   not null default '{0, 0, 0, 0, 0}'
);

insert into test(volume, price) 
values ('{1, 10, 50, 100, 250}', '{10, 9.75, 9.5, 9, 8.75}');

在 PostgreSQL 12 中没有解析异常,因为它似乎以前存在过(参见 Create array of custom domain postgres),但是,每当我尝试检索作为 tmoney[] 插入的值时,都会发现 DBCException。请注意,smallint[] 不会发生此错误。

select * from test;

id|volume           |price                                        |
--|-----------------|---------------------------------------------|
 1|{1,10,50,100,250}|DBCException: Can't resolve data type _tmoney|

https://www.postgresql.org/docs/current/sql-createdomain.html 的文档仅指定

tdata_type – 域的基础数据类型。这可以包括数组说明符。

这与创建的域一致

create domain tmoney as decimal (13,4)[];

create table test (
  id        int generated by default as identity primary key,
  volume    smallint[5] not null default '{0, 0, 0, 0, 0}',
  price     tmoney  not null default '{0, 0, 0, 0, 0}'
);

insert into test(volume, price) 
values ('{1, 10, 50, 100, 250}', '{10, 9.75, 9.5, 9, 8.75}');

select * from test;

id|volume           |price                   |
--|-----------------|------------------------|
 1|{1,10,50,100,250}|{10.0,9.75,9.5,9.0,8.75}|

但是,由于 PostgreSQL 12 解析器不会阻止在表声明中使用 tmoney[5],我想知道是否有不同的语法允许我使用自定义域的第一个版本。

【问题讨论】:

    标签: postgresql postgresql-12 sql-domain


    【解决方案1】:

    使用域数组是introduced in v11

    您的 SQL 语句与 psql 配合得很好。

    您必须使用可能无法正确支持该功能的其他客户端。 考虑使用该软件提交错误报告或增强请求。

    【讨论】:

    • 感谢 Laurenz Albe。我正在使用 DBeaver。是的,正如你所说,它适用于psql。很遗憾 DBeaver 失败了。
    • 今天,DBeaver v7.2.0 的发布已经修复了这个错误。
    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 2020-10-22
    • 2022-09-25
    • 2018-01-30
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 2023-01-04
    相关资源
    最近更新 更多