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

Fly*_*bus 5 sql postgresql ddl casting 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;
Run Code Online (Sandbox Code Playgroud)

Postgres给了我这个错误:

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

Erw*_*ter 27

使用:

ALTER TABLE products ALTER power_price TYPE bool USING (power_price::int::bool);
Run Code Online (Sandbox Code Playgroud)

numeric和之间没有定义直接强制转换boolean.你可以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;
Run Code Online (Sandbox Code Playgroud)

关于手册的详细信息ALTER TABLE.