postgres ALTER TYPE cannot vcast type

92 阅读1分钟

background:

CREATE TYPE special_key_type AS ENUM ('full', 'partial1', 'partial2', 'confirmation');

request: remove confirmation from ENUM

不可能从ENUM中去掉一个value,因为: Although enum types are primarily intended for static sets of values, there is support for adding new values to an existing enum type, and for renaming values (see ALTER TYPE). 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.

try:

CREATE TYPE "exit_name" AS ENUM ('full', 'partial1', 'partial2');

改column类型:

ALTER TABLE public.keyboard_special_keys ALTER COLUMN "exitName" TYPE "exit_name";

error: column "exitName" cannot be cast automatically to type exit_name

ALTER TABLE public.keyboard_special_keys ALTER COLUMN "exitName" TYPE "exit_name" USING "exitName"::"exit_name";

cannot cast type special_key_type to exit_name

reason: have to cast to text first, then to another type

correct solution:

ALTER TABLE public.keyboard_special_keys ALTER COLUMN "exitName" TYPE "exit_name" USING "exitName"::text::"exit_name";

devpress.csdn.net/postgresql/…