# MySQL 转 PG 的异常
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/02.%20%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/02.%20%E6%95%B0%E6%8D%AE%E5%BA%93/01.%20PostgreSQL/06.%20MySQL%E8%BD%ACPG%E5%BC%82%E5%B8%B8


# MySQL 转 PG 的异常

## update 更新时的转换异常信息**

```js
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` 的自动转换逻辑

```sql
-- 创建函数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;
```

```sql

-- 删除转换
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` 异常 就是在操作符比较的时候有多个转换逻辑不知道用哪个了，死循环了
