Error on ALTER TYPE in postgres relation does not exist

独自空忆成欢 提交于 2019-12-07 18:14:29

问题


Using the following:

CREATE TYPE user_types AS ENUM ('it', 'accounting', 'processes');

CREATE TABLE my_users
(
    my_user_id integer NOT NULL,
    my_user_name text NOT NULL,
    my_user_type user_types
)

I want to change one of the user types:

ALTER TYPE user_types RENAME ATTRIBUTE it TO softwaredev CASCADE;

I get a error:

ERROR: relation "user_types" does not exist
SQL state: 42P01

I tried adding quotes and backticks but that didn't help. The example I wrote down here is not the exact code, my type has 31 characters, but I don't think the length of my type is the issue.

I'm using postgres version 9.6.2


回答1:


ALTER TYPE ... RENAME ATTRIBUTE only works for composite types, not for ENUM types.

While there is a way to add new entries to such a type (ALTER TYPE ... ADD VALUE 'new_value'), there is no supported way to remove or rename an enumeration entry.

If you are not afraid to mess with the catalogs, you can try as superuser:

UPDATE pg_enum
SET enumlabel = 'softwaredev'
WHERE enumtypid = 'user_types'::regtype
  AND enumlabel = 'it';

From PostgreSQL v10 on, you can use

ALTER TYPE ... RENAME VALUE ... TO ...


来源:https://stackoverflow.com/questions/44243336/error-on-alter-type-in-postgres-relation-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!