创建于2024-08-27更新于 2025-03-22
MySQL 转 PG 的异常
update 更新时的转换异常信息**
Cause : org.postgre sql.util.PSQLException: ERROR: column "name" is of type smallint but expression is of type boolean
update from xx set name = false where name = true错误原因:在 update/insert 赋值语句的时候,字段类型是 smallint,但是传参却是布尔值类型
解决办法:
postgres 数据库添加 boolean <-> smallint 的自动转换逻辑
-- 创建函数1 smallint到boolean到转换函数
CREATE OR REPLACE FUNCTION "smallint_to_boolean"("i" int2)
RETURNS "pg_catalog"."bool" AS $BODY$
BEGIN
RETURN (i::int2)::integer::bool;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
-- 创建赋值转换1
create cast (SMALLINT as BOOLEAN) with function smallint_to_boolean as ASSIGNMENT;
-- 创建函数2 boolean到smallint到转换函数
CREATE OR REPLACE FUNCTION "boolean_to_smallint"("b" bool)
RETURNS "pg_catalog"."int2" AS $BODY$
BEGIN
RETURN (b::boolean)::bool::int;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
-- 创建隐式转换2
create cast (BOOLEAN as SMALLINT) with function boolean_to_smallint as implicit;
-- 删除转换
drop CAST (BOOLEAN as SMALLINT)
-- 删除函数
drop function boolean_to_smallint
-- 删除转换
drop CAST (SMALLINT as BOOLEAN)
-- 删除函数
drop function smallint_to_boolean
主要不要乱添加隐式转换函数,可能导致 Could not choose a best candidate operator 异常 和 # operator is not unique 异常 就是在操作符比较的时候有多个转换逻辑不知道用哪个了,死循环了