我有一个使用枚举类型的表列。我希望更新枚举类型有一个额外的可能值。我不想删除任何现有的值,只是添加新的值。最简单的方法是什么?
当前回答
DB::statement("ALTER TABLE users DROP CONSTRAINT users_user_type_check");
$types = ['old_type1', 'old_type1', 'new_type3'];
$result = join( ', ', array_map(function ($value){
return sprintf("'%s'::character varying", $value);
}, $types));
DB::statement("ALTER TABLE users ADD CONSTRAINT users_user_type_check CHECK (user_type::text = ANY (ARRAY[$result]::text[]))");
其他回答
注意:如果你正在使用PostgreSQL 9.1或更高版本,并且你可以在事务之外进行更改,请参阅下面的回答,以获得更简单的方法。
几天前我也遇到了同样的问题,发现了这个帖子。所以我的回答可以帮助那些正在寻找解决方案的人:)
如果只有一两个列使用想要更改的枚举类型,可以尝试这样做。此外,您还可以更改新类型中值的顺序。
-- 1. rename the enum type you want to change
alter type some_enum_type rename to _some_enum_type;
-- 2. create new type
create type some_enum_type as enum ('old', 'values', 'and', 'new', 'ones');
-- 3. rename column(s) which uses our enum type
alter table some_table rename column some_column to _some_column;
-- 4. add new column of new type
alter table some_table add some_column some_enum_type not null default 'new';
-- 5. copy values to the new column
update some_table set some_column = _some_column::text::some_enum_type;
-- 6. remove old column and type
alter table some_table drop column _some_column;
drop type _some_enum_type;
如有多于一栏,应重复3-6。
对于那些寻找事务内解决方案的人来说,下面的方法似乎有用。
不是ENUM,而是TEXT类型上的DOMAIN应使用,并约束检查该值是否在指定的允许值列表中(如某些注释所建议的)。唯一的问题是,如果一个域被任何复合类型使用,它就不能被添加约束(因此也不能被修改)(文档只是说这“最终应该得到改进”)。但是,可以使用调用函数的约束来解决这样的限制,如下所示。
START TRANSACTION;
CREATE FUNCTION test_is_allowed_label(lbl TEXT) RETURNS BOOL AS $function$
SELECT lbl IN ('one', 'two', 'three');
$function$ LANGUAGE SQL IMMUTABLE;
CREATE DOMAIN test_domain AS TEXT CONSTRAINT val_check CHECK (test_is_allowed_label(value));
CREATE TYPE test_composite AS (num INT, word test_domain);
CREATE TABLE test_table (val test_composite);
INSERT INTO test_table (val) VALUES ((1, 'one')::test_composite), ((3, 'three')::test_composite);
-- INSERT INTO test_table (val) VALUES ((4, 'four')::test_composite); -- restricted by the CHECK constraint
CREATE VIEW test_view AS SELECT * FROM test_table; -- just to show that the views using the type work as expected
CREATE OR REPLACE FUNCTION test_is_allowed_label(lbl TEXT) RETURNS BOOL AS $function$
SELECT lbl IN ('one', 'two', 'three', 'four');
$function$ LANGUAGE SQL IMMUTABLE;
INSERT INTO test_table (val) VALUES ((4, 'four')::test_composite); -- allowed by the new effective definition of the constraint
SELECT * FROM test_view;
CREATE OR REPLACE FUNCTION test_is_allowed_label(lbl TEXT) RETURNS BOOL AS $function$
SELECT lbl IN ('one', 'two', 'three');
$function$ LANGUAGE SQL IMMUTABLE;
-- INSERT INTO test_table (val) VALUES ((4, 'four')::test_composite); -- restricted by the CHECK constraint, again
SELECT * FROM test_view; -- note the view lists the restricted value 'four' as no checks are made on existing data
DROP VIEW test_view;
DROP TABLE test_table;
DROP TYPE test_composite;
DROP DOMAIN test_domain;
DROP FUNCTION test_is_allowed_label(TEXT);
COMMIT;
以前,我使用了一个类似于公认答案的解决方案,但一旦考虑视图、函数或复合类型(特别是使用其他视图的视图,使用修改过的enum…),它就远远不够好了。这个答案中提出的解决方案似乎在任何条件下都有效。
唯一的缺点是,当删除一些允许的值时,不会对现有数据执行检查(这可能是可以接受的,特别是对于这个问题)。(不幸的是,调用ALTER DOMAIN test_domain VALIDATE CONSTRAINT val_check最终会得到与向复合类型使用的域添加新约束相同的错误。)
请注意,轻微的修改,如CHECK (value = ANY(get_allowed_values()),其中get_allowed_values()函数返回允许值的列表,将不起作用-这很奇怪,所以我希望上面提出的解决方案可靠地工作(它对我来说,到目前为止…)。(它工作,实际上-这是我的错误)
来自Postgres 9.1文档:
ALTER TYPE name ADD VALUE new_enum_value [ { BEFORE | AFTER } existing_enum_value ]
例子:
ALTER TYPE user_status ADD VALUE 'PROVISIONAL' AFTER 'NORMAL'
如果你正在使用Postgres 12(或更高版本),你可以运行ALTER TYPE…在交易(文档)中增加价值。
如果ALTER TYPE…ADD VALUE(向枚举中添加新值的表单 类型)在事务块内执行,则新值不能为 一直使用到事务提交之后。
因此在迁移中不需要任何hack。
UPD:这里有一个例子(感谢Nick)
添加一个新值
一个可能的解决方案如下;前提条件是,使用的枚举值中没有冲突。(例如,当删除一个枚举值时,确保该值不再使用。)
-- rename the old enum
alter type my_enum rename to my_enum__;
-- create the new enum
create type my_enum as enum ('value1', 'value2', 'value3');
-- alter all you enum columns
alter table my_table
alter column my_column type my_enum using my_column::text::my_enum;
-- drop the old enum
drop type my_enum__;
同样,用这种方法列的顺序也不会改变。
推荐文章
- 映射enum在JPA与固定的值?
- 比较两个SQL Server数据库(模式和数据)的最佳工具是什么?
- PostgreSQL删除所有内容
- 为什么PostgreSQL要对索引列进行顺序扫描?
- PostgreSQL INSERT ON冲突更新(upsert)使用所有排除的值
- 使用Enum实现单例(Java)
- 如何检查一个表是否存在于给定的模式中
- 如何在SQL Server Management Studio中查看查询历史
- c++枚举类可以有方法吗?
- 如何将整数转换为字符串作为PostgreSQL查询的一部分?
- Psycopg2:用一个查询插入多行
- 如何为一个或枚举删除一个项目?
- PostgreSQL返回JSON数组的结果集?
- PostgreSQL通配符LIKE用于单词列表中的任何一个
- 如何创建数据库的MongoDB转储?