Cannot cast type numeric to boolean

筅森魡賤 提交于 2019-12-06 02:58:41

问题


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 gives me this error:

Query failed: ERROR: cannot cast type numeric to boolean


回答1:


Use:

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

There is no direct cast defined between numeric and boolean. You can use integer as middle-ground. text would be another candidate for middle ground, since every type can be cast from / to text. Values have to be 1 / 0 of course.

Better yet, do it all in a single command for better performance and shorter lock time:

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;

Details in the manual about ALTER TABLE.



来源:https://stackoverflow.com/questions/19290248/cannot-cast-type-numeric-to-boolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!