出现原因:项目使用的mysql数据库切换到postgres数据库,数据迁移过去,部分字段类型自动更改
具体操作:
1 查看当前数据库boolean类型的转换规则
可以看到,初始化数据库后,默认只有integer类型的显示转换。
select castsource::regtype,casttarget::regtype,castcontext,castfunc from pg_cast where castsource=‘boolean’::regtype;
2 修改integer类型的转换为隐式转换
update pg_cast set castcontext=‘i’ where castsource = ‘boolean’::regtype and casttarget=‘integer’::regtype;
3 添加boolean类型转换smallint类型的隐式转换
添加boolean类型转换smallint类型的自定义函数
create or replace function boolean2smallint(boolean) returns smallint as KaTeX parse error: Can't use function '$' in math mode at position 15: begin if $̲1 = true then … language plpgsql;
添加boolean类型转换smallint类型的隐式转换规则
create cast(boolean as smallint) with function boolean2smallint(boolean) as implicit;
4.修改完成后,再次查看当前数据库boolean类型的转换规则,可以看到,boolean类型有了转换integer和smallint类型的隐式转换规则。
文献来源:
原文链接:https://blog.csdn.net/weixin_43128854/article/details/130759812