I have a postgres enum that would like to change its default sort order
I learned about the pg_enum type enumsortorder but haven't found any documentation on how to change the order.
The enum I have for context:
CREATE TYPE day AS ENUM (
'everyday',
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday'
);
Currently when I retrieve ordered by day I get from 'everyday' to 'saturday'. I would like to get from Monday-Sunday and have Everyday as the last value.
Is this possible?
You can only replace the type with a new one, which can be a bit laborious. Eg:
BEGIN;
-- drop views, functions which are using day
ALTER TYPE day RENAME TO day_old;
CREATE TYPE day AS ENUM (
'everyday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday'
);
ALTER TABLE x ALTER my_day TYPE day USING my_day::TEXT::day;
ALTER TABLE y ALTER your_day TYPE day USING your_day::TEXT::day;
-- etc. etc.
-- Recreate views, functions which are using day
DROP TYPE day_old;
END;
That's not possible.
Existing values cannot be removed from an enum type, nor can the sort ordering of such values be changed , short of dropping and re-creating the enum type
(Emphasis mine)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.