sql - 不兼容的类型 : numeric and bigint. 在 PostgreSQL 中

标签 sql oracle postgresql hibernate jpa

在迁移到 postgresql 时,我面临以下问题

键列 veh_reg_authorityid 的类型不兼容:numericbigint。 以下是映射:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VEH_REG_AUTHORITY", columnDefinition ="Numeric(12,0)")
private VehicleRegistrationAuthority vehicleRegistrationAuthority;

VehicleRegistrationAuthority 具有 Long 类型的 id,因此根据 PostgreSQL 将其转换为 BIGINT

所以一个简单的解决方案是相应地更改 columnDefinition ="Numeric(12,0)",但我们不能这样做,因为它适用于 oracle 和 mysql,并且它在输出代码中被大量使用,因此不可能改变。

以下是 Oracle、Mysql 和 PostgreSQL 中的类型:

column                          java type             Oracle     Mysql   PostgreSQL
id                               Long                 NUMBER     BIGINT  BIGINT
veh_reg_authority VehicleRegistrationAuthority        NUMBER   decimal(12,0) NUMERIC

创建外键 hibernate 时出现以下问题。

Caused by: org.postgresql.util.PSQLException: ERROR: foreign key constraint "fk7gyqskn6x2l2910xhhtjgvh0j" cannot be implemented
Detail: Key columns "veh_reg_authority" and "id" are of incompatible types: numeric and bigint.

最佳答案

如果要定义外键约束,则应为两列使用相同的数据类型。

如果您的应用程序在 Oracle 中对两列都使用数据类型 NUMBER 工作正常,那么如果您对两个列都使用 numeric ,为什么它在 PostgreSQL 中不能工作?这些数据类型是可比较的。

关于sql - 不兼容的类型 : numeric and bigint. 在 PostgreSQL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45184232/

相关文章:

sql - 使用设备的最新数据对一系列设备标记的数据求和

java - Oracle 中的什么数据类型会映射到 Java long?

oracle - 流利的NHibernate-如何为Oracle配置?

postgresql - 玩进化转义表名

postgresql - 使用 node-postgres 获取函数的返回类型 OID

mysql - 显示零值查询

php - 无法从数据库回显表

mysql - 连接多个表,不重复行

oracle - 如何向 Hibernate @ManyToMany 关系添加 where 子句?

mysql - 将数据库逻辑放在应用程序中,而不是触发器、存储过程、约束等