简体   繁体   English

无法在自定义类型的列上使用替换功能

[英]Unable to use Replace function on column of custom type

In order to enforce some constraints, our devs use a custom type for a type/factor column. 为了实施一些约束,我们的开发人员将自定义类型用于type / factor列。

Custom type defined as such and includes both the old and new value: 这样定义的自定义类型,包括旧值和新值:

create type custom_type as enum ('Value1', 'Value2', 'Value3', 'OldValue', 'NewValue');

I'm trying to update some values in a table like so: 我试图像这样更新表中的一些值:

UPDATE table SET column = replace(column, 'OldValue'::custom_type, 'NewValue'::custom_type);

However am receiving the following error: 但是我收到以下错误:

[2019-04-11 16:37:42] [42883] ERROR: function replace(column, custom_type, custom_type) does not exist
[2019-04-11 16:37:42] Hint: No function matches the given name and argument types. You might need to add explicit type casts.

I'm guessing that this is going to require defining a custom "replace" function in order to handle the types or is there a way that I can handle this by just casting. 我猜想这将需要定义一个自定义的“替换”函数以处理类型,或者是否有一种方法可以通过强制转换来处理它。

For reference this is PostgreSQL 11.2 供参考,这是PostgreSQL 11.2

replace() works on strings, not on enums. replace()适用于字符串,而不适用于枚举。 You can do what you appently want with an UPDATE with a WHERE clause. 您可以使用带有WHERE子句的UPDATE来完成您想做的事情。

UPDATE table
       SET column = 'NewValue'::custom_type
       WHERE column = 'OldValue'::custom_type;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM