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';

参考:https://blog.csdn.net/joshho/article/details/110090297