跳到主要内容

PostgreSQL 类型不匹配查询报错

错误原因

因传参与数据库类型不匹配导致查询数据库报错,错误日志如下:

ERROR: operator does not exist: bigint = character varying
建议:No operator matches the given name and argument types. You might need to add explicit type casts.
位置:142

解决方案

在PostgreSQL JDBC连接中添加 stringtype=unspecified 参数

spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://127.0.0.1:5432/${database}?stringtype=unspecified
username: ${username}
password: ${password}

default-database-product-name: postgresql

核心作用:让驱动不再把JavaString强制当作 VARCHAR传给数据库,而是以"无类型(unknown)"形式发送,由PostgreSQL根据目标字段类型自动推断、隐式转换。