Converting an Integer to Enum in PostgreSQL

前端 未结 3 1038
终归单人心
终归单人心 2021-02-20 15:09

I have created a custom data type enum like so:

create type \"bnfunctionstype\" as enum ( 
    \'normal\', 
    \'library\', 
    \'import\', 
    \'thunk\', 
           


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 15:43

    If you have an enum like this:

    CREATE TYPE payment_status AS ENUM ('preview', 'pending', 'paid', 
                                        'reviewing', 'confirmed', 'cancelled');
    

    You can create a list of valid items like this:

    SELECT i, (enum_range(NULL::payment_status))[i] 
      FROM generate_series(1, array_length(enum_range(NULL::payment_status), 1)) i
    

    Which gives:

     i | enum_range 
    ---+------------
     1 | preview
     2 | pending
     3 | paid
     4 | reviewing
     5 | confirmed
     6 | cancelled
    (6 rows)
    

提交回复
热议问题