无法将类型数字转换为布尔值

时间:2013-10-10 08:04:08

标签: sql postgresql casting ddl alter-table

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给了我这个错误:

  

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

1 个答案:

答案 0 :(得分:22)

使用:

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

的详细信息