0

I am using enum in Postgresql. I have used a enum called city in my database. My requirement is how to get a enum by using conditional statement. I used the query

SELECT 'Bengaluru'::CITY as TEXT 

it works fine but if the text is not found in enum then it throws a error. How should I make/change the sql query so that it doesn't throw the error.

5
  • 2
    enums are a design anti-pattern. Use a proper lookup table instead. Then you can easily run a statement that checks if the value exist that will not result in an error Commented Jan 29, 2019 at 7:09
  • what do you mean by lookup table ? how to create it. @a_horse_with_no_name Commented Jan 29, 2019 at 7:10
  • A standard one-to-many relationship: a separate city table e.g. create table city (id integer primary key, name text not null). Then use a foreign key referencing the city table instead. Commented Jan 29, 2019 at 7:11
  • I previously used the same but we don't want city in separate table. We are planning to use Enum for that. @a_horse_with_no_name Commented Jan 29, 2019 at 7:27
  • Are you aware that enums violate normalization? Are you aware about the restrictions imposed by enums? Commented Jan 29, 2019 at 7:28

2 Answers 2

2

You need to check the system tables for that. Assuming an enum defined as:

create type city as enum ('Munich', 'Berlin'); 

To find out if a specific label was defined for an enum type, you can use the following:

select exists (select *
                from pg_enum e
                  join pg_type t on t.oid = e.enumtypid
                  join pg_namespace s on s.oid = t.typnamespace
                where s.nspname = 'public' --<< adjust here for the correct schema
                  and t.typname = 'city'
                  and e.enumlabel = 'Bengaluru');

The correct solution is to not use enums, but a proper one-to-many relationship instead.

create table city
(
  id integer primary key, 
  name varchar(100) not null unique --<< the "unique" mimics the enuma
); 

insert into city (id, name)
values (1, 'Munich'), (2, 'Berlin');

To use the "enum", use a foreign key:

create table some_table_using_city
(
   ... other columns ...
   city_id integer references city
);

To check if a city name exists, simply use:

select id
from city
where name = 'Bengaluru';
Sign up to request clarification or add additional context in comments.

Comments

0
SELECT 
    CASE WHEN 'Bengaluru'::CITY !=NULL THEN 'Bengaluru'::CITY ELSE "Not Found" 
    END as TEXT

CASE can be used here.

For more details regarding syntax you can find here.

4 Comments

How? If value not present then it will return string "Not Found" instead of error. Don't you think same?
No, the cast 'Bengaluru'::CITY will result in "invalid input value for enum city: "Bengaluru"". Additionally: you can not use != (or <>) to compare with null and string constants need to be enclosed in single quotes "Not Found" refers to a column.
It gives error. ERROR: syntax error at or near "THEN" LINE 1: SELECT 'Bengaluru'::CITY is not NULL THEN 'Bengaluru'::CITY @iamrajshah
still got the error please check the query once @a_horse_with_no_name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.