【问题标题】:Cannot cast type numeric to boolean无法将类型数字转换为布尔值
【发布时间】:2013-10-10 08:04:08
【问题描述】:
ALTER TABLE products ALTER COLUMN power_price DROP DEFAULT;
ALTER TABLE products ALTER COLUMN power_price TYPE bool USING (power_price::boolean);
ALTER TABLE products ALTER COLUMN power_price SET NOT NULL;
ALTER TABLE products ALTER COLUMN power_price SET DEFAULT false;

Postgres 给我这个错误:

查询失败:错误:无法将类型数字转换为布尔值

【问题讨论】:

    标签: sql postgresql casting ddl alter-table


    【解决方案1】:

    用途:

    ALTER TABLE products ALTER power_price TYPE bool USING (power_price::int::bool);
    

    numericboolean 之间没有定义直接转换。您可以使用integer 作为中间立场。 text 将是另一个中间立场的候选者,因为每种类型都可以从 / 转换为 text。当然,值必须是 1 / 0

    更好的是,在一个命令中完成所有操作以获得更好的性能和更短的锁定时间:

    ALTER TABLE products
      ALTER power_price DROP DEFAULT
     ,ALTER power_price TYPE bool USING (power_price::int::bool)
     ,ALTER power_price SET NOT NULL
     ,ALTER power_price SET DEFAULT false;
    

    手册中有关ALTER TABLE的详细信息。

    【讨论】:

    • 注意“当然,值必须是 1 / 0”。实际上值可以是任何值。 0 为假,其他为真。
    • 选择 12 :: INT :: BOOLEAN
    • @Mahou5:我引用的声明仅适用于text 作为垫脚石。考虑:SELECT 12::text::bool; -> ERROR: invalid input syntax for type boolean: "12"。所以可以选择在演员阵容中是否严格......
    • 你是对的。我只是根据使用“INT”作为中间立场来指出这一点。
    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    相关资源
    最近更新 更多