Postgres将列的整数转换为布尔值 [英] Postgres Alter Column Integer to Boolean

查看:1883
本文介绍了Postgres将列的整数转换为布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字段是INTEGER NOT NULL DEFAULT 0,我需要将其更改为bool。

I've a field that is INTEGER NOT NULL DEFAULT 0 and I need to change that to bool.

这是我使用的:

ALTER TABLE mytabe 
ALTER mycolumn TYPE bool 
USING 
    CASE 
        WHEN 0 THEN FALSE 
        ELSE TRUE 
    END;

但我得到:

ERROR:  argument of CASE/WHEN must be type boolean, not type integer

********** Error **********

ERROR: argument of CASE/WHEN must be type boolean, not type integer
SQL state: 42804

任何想法?

谢谢。

推荐答案

试试这个:

ALTER TABLE mytabe ALTER COLUMN mycolumn DROP DEFAULT;
ALTER TABLE mytabe ALTER mycolumn TYPE bool USING CASE WHEN mycolumn=0 THEN FALSE ELSE TRUE END;
ALTER TABLE mytabe ALTER COLUMN mycolumn SET DEFAULT FALSE;

您需要首先删除约束(因为它不是布尔值),其次您的 CASE 语句的语法错误。

You need to remove the constraint first (as its not a boolean), and secondly your CASE statement was syntactically wrong.

这篇关于Postgres将列的整数转换为布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆