How to create an enumeration field with a default value?

types = { # add your custom types here 'attendance': ('Notconfirmed','Coming', 'Notcoming', 'Maycome',), } CREATE TYPE attendance AS ENUM types; 

The above query creates an enumeration type attendance with enumerations specified in the types. How to create a type with a default label? In this case, I want to create an attendance type with a default value of Notconfirmed.

+6
source share
2 answers

I tried to do the same thing as you, and only got the answer in stackoverflow. You can create ENUM with a default value. This is what I got for you.

 CREATE TYPE status AS ENUM ('Notconfirmed','Coming', 'Notcoming', 'Maycome'); CREATE TABLE t ( id serial, s status default 'Notconfirmed' -- <==== default value ); INSERT INTO t(id) VALUES (default) RETURNING *; 

It worked for me like a charm.

+29
source

In addition to the words of Sudarshan ...

If someone needs an example in another scheme:

 CREATE TABLE schema_name.table_name ( -- id serial, s schema_name.type_name default 'Notconfirmed'::schema_name.type_name ); 
0
source

Source: https://habr.com/ru/post/957740/


All Articles