在迁移到 postgresql 时,我面临以下问题
键列 veh_reg_authority
和 id
的类型不兼容:numeric
和 bigint
。
以下是映射:
@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/