【发布时间】:2018-02-26 16:04:03
【问题描述】:
我们正在尝试将 numeric(10,2) 列类型更改为 numeric(17,2) 类型。当我们用幼稚的方法来做这件事时:
ALTER TABLE table_a ALTER COLUMN col_a SET DATA TYPE numeric(17,2);
我们收到不理解的警告:
WARNING: 1 attrdef record(s) missing for rel table_a
WARNING: generating possibly-non-unique OID for "pg_attrdef"
列类型确实正确更改为numeric(17,2)(至少\d table_a 告诉我们),我看不出结果表有任何问题,但警告仍然存在。
我们的表定义类似于:
Table "public.table_a"
Column | Type | Collation | Nullable | Default
-------------------------+------------------------+-----------+----------+---------
col_a | numeric(10,2) | | not null | 0.00
在查阅了 postgres 文档后,我们通过首先删除 Default 约束来尝试另一列:
ALTER TABLE table_a ALTER COLUMN col_b DROP DEFAULT;
ALTER TABLE table_a ALTER COLUMN col_b SET DATA TYPE numeric(17,2);
ALTER TABLE table_a ALTER COLUMN col_b SET DEFAULT 0.00;
导致更多警告:
WARNING: 2 attrdef record(s) missing for rel table_a
WARNING: generating possibly-non-unique OID for "pg_attrdef"
正如您在对另一列运行查询后所见,我们现在在 pg_attrdef 中缺少 2 条记录。
运行查询时,警告似乎会持续存在并随机显示。
我们检查了pg_attrdef 表,看看是否可以发现任何发现adbin 内部表示中的:location 字段已设置为-1:
adrelid | 37294
adnum | 37
adbin | {FUNCEXPR :funcid 1703 :funcresulttype 1700 :funcretset false :funcvariadic false :funcformat 2 :funccollid 0 :inputcollid 0 :args ({CONST :consttype 1700 :consttypmod -1 :constcollid 0 :constlen -1 :constbyval false :constisnull false :location -1 :constvalue 6 [ 24 0 0 0 0 -127 ]} {CONST :consttype 23 :consttypmod -1 :constcollid 0 :constlen 4 :constbyval true :constisnull false :location -1 :constvalue 4 [ 6 0 17 0 0 0 0 0 ]}) :location -1}
adsrc | 0.00
但是编译它:
select pg_get_expr(pa.adbin, 'table_a'::regclass) from pg_attrdef pa where adrelid = 37294 and adnum = 37;
工作并返回正确的0.00 值。
一些进一步的背景信息:该表由 Hibernate 生成,列定义如下:
@Column(nullable = false, columnDefinition = "Decimal(10,2) default '0.00'")
private Double col_a = 0.0;
我正在 PostreSQL 10.0 数据库上运行查询,但想稍后在 8.4 数据库上运行。
有人可以帮助我们弄清楚这些警告的含义以及我们如何避免它们吗?
【问题讨论】:
标签: sql postgresql hibernate