数据库系列:postgresql中boolean字段与smallint字段的自动转换

1、使用postgre账号进入到相应的模式下执行:

CREATE OR REPLACE FUNCTION boolean_to_smallint(b boolean) RETURNS smallint AS $$
    BEGIN
            RETURN (b::boolean)::bool::int;
    END;
$$LANGUAGE plpgsql;

CREATE CAST (boolean AS smallint) WITH FUNCTION boolean_to_smallint(boolean) AS implicit;

2、测试:(is_leader字段为smallint类型)

UPDATE public."user"
        SET is_leader=True
        WHERE id='125';


posted @ 2023-03-23 21:25  菜鸟辉哥  阅读(296)  评论(0编辑  收藏  举报