当前位置:   article > 正文

postgresql 类型转化时遇到的问题_pgsql 报错default for column cannot be cast automati

pgsql 报错default for column cannot be cast automatically to type json
postgres=# alter table test alter config type jsonb;
ERROR:  column "config" cannot be cast automatically to type jsonb 
HINT:  Specify a USING jsonb to perform the conversion.
  • 1
  • 2
  • 3

之前config 字段类型为 hstore,怎么才能转化呢,如下三种方式都可以:
第一种

alter table test alter config type jsonb using config::jsonb;
  • 1

第二种

alter table test alter config type jsonb using(config::jsonb);
  • 1

第三种

alter table test alter config type jsonb using cast(config as jsonb);

#test case
select cast('12' as int);
select '12'::int;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果是int 类型变更为 varchar 可以不用显示的using 方式去变更,这个与类型的强弱有关系, 如 5/2.0 其实计算机会把5先转成与2.0一样的浮点类型,然后再做除法, 如果想要5/2.0得到整数,则这样才能获得:(int)5/2.0 。
注:5/2.0的类型转化是C语言例子

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/870388
推荐阅读
  

闽ICP备14008679号