Postgresql踩坑 | ERROR: operator does not exist: uuid = character varying

业务场景:

在MyBatis中对Postgresql数据库的表数据进行update操作,报以下错误:

  • Caused by: org.postgresql.util.PSQLException: ERROR: column “data” is of type jsonb but expression is of type character varying
    建议:You will need to rewrite or cast the expression.

    Postgresql踩坑 | ERROR: operator does not exist: uuid = character varying

  • Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = character varying
    建议:No operator matches the given name and argument types. You might need to add explicit type casts.

    Postgresql踩坑 | ERROR: operator does not exist: uuid = character varying


原因:

由于Postgresql数据库的数据类型跟MySQL不一致,导致惯性思维在MyBatis数据库中写sql语句,导致报错。原因其实是同一个,就是接受的参数的数据类型和Postgresql数据库的不一致。

我这里的uuiddata分别是uuid类型和jsonb类型,如下图:
Postgresql踩坑 | ERROR: operator does not exist: uuid = character varying

然后MyBatis中的sql如下:
Postgresql踩坑 | ERROR: operator does not exist: uuid = character varying
场景就是这样。


解决方法:

在每个类型后面加上对应的类型转换,如uuid类型和jsonb,就在对应的参数后面加上::类型这样的格式即可,如下:
Postgresql踩坑 | ERROR: operator does not exist: uuid = character varying

至此,问题解决!