Postgresql change column type from int to UUID

后端 未结 7 1585
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 13:26

I\'d like to change the column type from an int to a uuid. I am using the following statement

ALTER TABLE tableA ALTER COLUMN colA          


        
相关标签:
7条回答
  • 2020-12-23 14:31

    I'm bumping to this after a long time, but there is a way to convert your integer column to a UUID with some kind of backwards-compatibility, namely keeping a way to have a reference to your old values, rather than dropping your values. It comprises of converting your integer value to a hex string and then padding that with necesary zeroes to make up an artificial UUID.

    So, assuming your current integer column is named ColA, the following statement would do it (mind the using part):

    ALTER TABLE tableA ALTER COLUMN ColA SET DATA TYPE UUID USING LPAD(TO_HEX(ColA), 32, '0')::UUID;
    
    0 讨论(0)
提交回复
热议问题