Ruby Notes« an older post

View enums in postgres

Snippet

enums help verify data is valid in the database by limiting what specific values can be saved to particular fields. Yay!

Viewing them, however, if you don't know what they are, isn't obvious. Use enum_range and unnest

development=> \d authors
                                          Table "public.authors"
   Column    |              Type              | Collation | Nullable |               Default               
-------------+--------------------------------+-----------+----------+-------------------------------------
 id          | bigint                         |           | not null | nextval('authors_id_seq'::regclass)
 email       | character varying              |           |          | 
 author_role | author_role                    |           | not null | 'external'::author_role
 name        | character varying              |           |          | 
...
 
development=> SELECT enum_range(NULL::author_role)
development-> ;
      enum_range       
-----------------------
 {external,fte,intern}
(1 row)
 
development=> SELECT unnest(enum_range(NULL::author_role));
  unnest  
----------
 external
 fte
 intern
(3 rows)

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.